nicbono
nicbono

Reputation: 11

How to reference bool in a script attached to a prefab UNITY 3D

I am a newbie C# coder.

Trying to access a bool which will initiate to true when first called.
The second script will look to it to see if it should initiate another item.
If the bool is set to false, it will initiate a new item;
if the bool is true, the second script will not run the if statement.

OLD CORE QUESTION:

Can someone please show me what the statement / procedure of referencing is? I am struggling to apply many of the available tutorials and Q&As to my problem.

UPDATE01:

I have identified what I think the core of the problem is. I am trying to access the class fruitCheck from a prefab, which does not begin in the scene, but is called only when a random countdown time has elapsed. Because of this I believe the bool becomes void, including a simple Debug.Log within the fruitCheck class. Does anyone have any advice on how I should overcome this problem? I would like to keep the count down time so that the time between each item is falling is spaced out/ random.

UPDATE02: Please refer to the solution to my question below.

BOOL INITIATE SCRIPT:

    public class fruitCheck : MonoBehaviour {

    //Check to see if object is falling...
    public static bool objectFalling;

    // Update is called once per frame
    public void Start () 
    {
        objectFalling = true;
        //Debug does not appear in the Console
        Debug.Log("Object IS Falling");
    }

    void Update() 
    {
        //Check if fruit has come in contact with deadZone.
        //If fruit has come in contact with dead zone then objectFalling = false;

        if(objectFalling == false)
        {
            //Debug does not appear in the Console
            Debug.Log("Object IS Falling");
        }
    }
}

THE IF STATEMENT WHERE I WOULD LIKE TO ACCESS BOOL:

public void Update () 
{

    //This Debug runs fine.
    Debug.Log("...check fruit...");

    if (fruitCheck.objectFalling == true)
    {
        timeLeftUntilSpawn = Time.time - startTime;

        if (timeLeftUntilSpawn >= secondsBetweenSpawn) 
        {
            startTime = Time.time - Random.Range(0.2f, 0.9f);
            //timeLeftUntilSpawn = Time.time - startTime;
            //timeLeftUntilSpawn = 0;
            Debug.Log ("IF STATEMENT SUCCESS: Spawn one here");

            SpawnRandomObject();
        }
    }

}

Upvotes: 0

Views: 1433

Answers (1)

nicbono
nicbono

Reputation: 11

I would first like to thank everyone who read through and took time to think about my question as it was complex, poorly described and difficult to conceptualise.

I came to a solution with my question. Please find below what I learned and how I understand it.

Because I am using prefabs, and they don't begin in the scene, a lot of what I wanted to execute would not. I did some further research and found out about AWAKE FUNCTIONS.

AWAKE FUNCTIONS: Awake is called when the script instance is being loaded. (Source below)

The awake function differs from the start function (which is where I set my bool variables to change at the beginning of the game) because it will be called as the game is loading, despite what is in the scene or not.

By changing to an awake function I was able to call a bool true which then set off the rest of my scripts successfully and allowed my core game mechanic to work.

Again, thank you for reading. If you have any further questions please feel free to contact me.

Cheers. nicbono.

Awake function source: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html

Start function source: http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

Upvotes: 1

Related Questions