GameObject.Find, When it is Called?

Hey I need your help guys.

Can you tell me if GameObject.Find is called when The Game Only Starts or when you want it.

Cuz Im searching for some gameObjects, and they are not found

public Vector3 blueScale;
public Vector3 orangeScale;
public Vector3 yellowScale;
public Vector3 purpleScale;
public Vector3 greenScale;
public Vector3 redScale;
public float blueEnergy;
public float greeEnergy;
public float orangeEnergy;
public float purpleEnergy;
public float redEnergy;
public float yellowEnergy;
public  Transform blue ;
public  Transform green;
public  Transform orange;
public  Transform purple;
public  Transform red;
public  Transform yellow;
public  GameObject _blue;
public  GameObject _green;
public  GameObject _orange;
public  GameObject _purple;
public  GameObject _red;
public  GameObject _yellow;
void Start()
{
    FindItemsObjects (out _blue);
}
void Update()
{
    //Debug.Log (_blue);
}
void OnGUI()
{
    if(GUI.Button(new Rect(10,120,100,50), "New Level"))
    {  
        FindItemsObjects(out _blue);
    }
}
public void FindItemsObjects(out GameObject _sds)
{
    Debug.Log ("Here Im searching");
    _sds = GameObject.FindWithTag ("Blue");
    WhenYouDoSomething (_sds, out blue, out blueScale);
    Debug.Log (_sds);
    _green = GameObject.FindWithTag ("Green");
    WhenYouDoSomething (_green, out green, out greenScale);
    _orange = GameObject.FindWithTag ("Orange");
    WhenYouDoSomething (_orange, out orange, out orangeScale);
    _purple = GameObject.FindWithTag ("Purple");
    WhenYouDoSomething (_purple, out purple, out purpleScale);
    _red = GameObject.FindWithTag ("Red");
    WhenYouDoSomething (_red, out red, out redScale);
    _yellow = GameObject.FindWithTag ("Yellow");
    WhenYouDoSomething (_yellow, out yellow, out yellowScale);
}
public Transform FindChild(GameObject parent, string name)
{
    if (parent.name == name)
    {
        Debug.Log("Parent's Name is same as you are searching for");
        return null;
    }
    Transform pTransform = parent.GetComponent<Transform> ();
    foreach (Transform t in pTransform)
    {
        if (t.name == name)
        {
            return t;
        }
    }
    return null;
}
public void WhenYouDoSomething(GameObject item, out Transform itemTransform, out Vector3 itemScale)
{
    if (item == null)
    {
        Debug.Log ("Item is null");
        itemTransform = null;
        itemScale = Vector3.zero;
    }
    else
    {
        itemTransform = FindChild (item, "line");
        itemScale = itemTransform.localScale;
    }
}

as you can see When I start the game, and put in the Update Function a Debug.Log of blue, the Log will give me a value. But after I press new Level, the blue value will be null.

Does this mean that it’s not searching for other values after the game is started?

need HELLP!!

It is just a function that returns a GameObject instead of void. So it is called when ever you need it to, just like any function.

I’m going to test your code. See if I can get it working

k

Well, that’s weird I put it in UpdateFunction to call it everyTime, and now it’s working.

I tested and watched the Status and there are not changes, I think I will go with this for now.

Ok awesome. Anyways I played around with it and managed to get it working with the button click. Basically your child object “line” wasn’t being found. Now it is:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    public Vector3 blueScale;
    public Vector3 orangeScale;
    public Vector3 yellowScale;
    public Vector3 purpleScale;
    public Vector3 greenScale;
    public Vector3 redScale;
    public float blueEnergy;
    public float greeEnergy;
    public float orangeEnergy;
    public float purpleEnergy;
    public float redEnergy;
    public float yellowEnergy;

    public  Transform blue ;
    public  Transform green;
    public  Transform orange;
    public  Transform purple;
    public  Transform red;
    public  Transform yellow;


    public  GameObject _blue;
    public  GameObject _green;
    public  GameObject _orange;
    public  GameObject _purple;
    public  GameObject _red;
    public  GameObject _yellow;

    void Start() {
        FindItemsObjects (out _blue);
    }

    void OnGUI() {
        if(GUI.Button(new Rect(10,120,100,50), "New Level"))
            FindItemsObjects(out _blue);
    }

    public void FindItemsObjects(out GameObject _sds) {
        Debug.Log ("Here Im searching");
        _sds = GameObject.FindWithTag ("Blue");
        WhenYouDoSomething (_sds, out blue, out blueScale);
        Debug.Log (_sds);

        _green = GameObject.FindWithTag ("Green");
        WhenYouDoSomething (_green, out green, out greenScale);

        _orange = GameObject.FindWithTag ("Orange");
        WhenYouDoSomething (_orange, out orange, out orangeScale);

        _purple = GameObject.FindWithTag ("Purple");
        WhenYouDoSomething (_purple, out purple, out purpleScale);

        _red = GameObject.FindWithTag ("Red");
        WhenYouDoSomething (_red, out red, out redScale);

        _yellow = GameObject.FindWithTag ("Yellow");
        WhenYouDoSomething (_yellow, out yellow, out yellowScale);
    }

    public void WhenYouDoSomething(GameObject item, out Transform itemTransform, out Vector3 itemScale) {
        if (item == null) {
            Debug.Log ("Item is null");
            itemTransform = null;
            itemScale = Vector3.zero;
        } else {
            itemTransform = findChild (item.transform, "line").transform;
            itemScale = itemTransform.localScale;

            Debug.Log(item.name.ToString () + " Item Scale: " + itemScale);
        }
    }

    public Transform findChild (Transform parent, string childName) {
        return parent.FindChild (childName);
    }
}
1 Like