Home > Back-end >  Problem with public bool fonction in unity
Problem with public bool fonction in unity

Time:05-10

Hello im new to c# and unity (so please dont be to rough with my shitty code) I have porblem with my public bool variable here is my code

DEATH SCRIPT(placed on a left to right moving block)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class death : MonoBehaviour 
{
    IEnumerator Wait()
    {
        
        yield return new WaitForSeconds(1);
        SceneManager.LoadScene("level 4");
    }
    void OnTriggerEnter(Collider col) 
    {
        if(col.name == "Sphere") 
        {   
            player_script.IsPlaying = false;
            StartCoroutine(Wait());
        }      
    }
}

Player script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player_script : MonoBehaviour
{
    public float velocity = 5f;
    public bool IsPlaying = true;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void FixedUpdate(){ 
        if(IsPlaying == true) {
            transform.Translate(Vector3.forward * velocity * Time.fixedDeltaTime * Input.GetAxis("Vertical"));
            transform.Translate(Vector3.right * velocity * Time.fixedDeltaTime * Input.GetAxis("Horizontal"));
        }
        
    }

here my problem I want to make a variable that will make me able to turn on and off the possibility to move but when I try to run this code it gives me this error:

Assets\script\death.cs(18,13): error CS0120: An object reference is required for the non-static field, method, or property 'player_script.IsPlaying'

I've tried to add public bool on the death script on line 18 but it just gives me those errors:

Assets\script\death.cs(19,34): error CS1001: Identifier expected

Assets\script\death.cs(21,5): error CS1022: Type or namespace definition, or end-of-file expected

Assets\script\death.cs(22,1): error CS1022: Type or namespace definition, or end-of-file expected

thank you every help would be appreciated

CodePudding user response:

You are not referencing player_script in your death script. To get a reference to player_script in death script, initialize player_script variable in your death script

public player_script player_script_gameobject;

Now, in the inspector of the game object with the death script, a slot can be seen in the death script. Drag the game object with the player_script script into that slot. Now you have successfully referenced player_script in your death script and you can access its public variables.

Hence, now the public variable IsPlaying can be accessed and modified at line 18 as

player_script_gameobject.IsPlaying = false;

CodePudding user response:

Hello and I'm glad you joined us. The player_script in death script actually calls the class type, while you need an instance of it that is your player. I think since you are a beginner it may be a little difficult to understand And better use the easier way. so add simple static keyword behind the IsPlaying variable to solve this problem.

public static bool IsPlaying = true;

Remember that the static variable is defined for the class, so do not use the player_script code for units other than the main player.

  • Related