How to disable line of code after certain if statement

So iam having some trouble. I have and object in scene that is alwais active called Scene manager it has these lines of code

public GameObject player;

void Start ()
    {
        Instantiate (player, spawpoint.position, spawpoint.rotation);
}

and i have another script that is alwais active whic has all the references and most of game logic for that scene but for them to work i need the player and a child game object that is in the player and i was try’ing this

public GameObject player ;
    public GameObject guns; // Guns are the players children

    void Start ()
    {
        player = GameObject.Find ("Player");
        guns = transform.Find ("Guns").gameObject;

    }

but i think the scene managers code starts first and it cand find fast enough in start fucntion so how can i find the player and gun’s in update and then disable that finding fuction ? because i read that if gameobject.find is runing in update it gives bad performence

One example:

public GameObject player ;
    public GameObject guns; // Guns are the players children
    private bool hasPlayerBeenFound;
    void Start ()
    {
    }
   
    void Update ()
    {
        if (!hasPlayerBeenFound){       
            player = GameObject.Find ("Player");
            guns = transform.Find ("Guns").gameObject;
            if (player != null && guns != null)
            {
                hasPlayerBeenFound = true;
            }
            else
            {
                return;
            }
        }
    }

I added the return because you said these variables are needed to use this script further on.

1 Like

Put the scene managers code in awake then it will run first.

Or this :slight_smile:

GameObject.Find is generally bad no matter what(Update is not a good idea). What would be better is when you create the player, you assign that player to the proper variable instead of having to go find it later.

thank you :slight_smile:

yeah but i need it to be able to respaw (instantiate) and when it does the values get lost it needs to find

i have try’d that doesnt work

If your player “dies” you don’t have to instantiate again. Just reset the player. Or, when you do a new instantiate, you reassign the new player object.