Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to disable line of code after certain if statement

Discussion in 'Scripting' started by kilix19980, May 12, 2017.

  1. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    So iam having some trouble. I have and object in scene that is alwais active called Scene manager it has these lines of code

    Code (CSharp):
    1. public GameObject player;
    2.  
    3. void Start ()
    4.     {
    5.         Instantiate (player, spawpoint.position, spawpoint.rotation);
    6. }
    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
    Code (CSharp):
    1. public GameObject player ;
    2.     public GameObject guns; // Guns are the players children
    3.  
    4.     void Start ()
    5.     {
    6.         player = GameObject.Find ("Player");
    7.         guns = transform.Find ("Guns").gameObject;
    8.  
    9.     }
    10.  
    11.  
    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
     
  2. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    One example:

    Code (CSharp):
    1. public GameObject player ;
    2.     public GameObject guns; // Guns are the players children
    3.     private bool hasPlayerBeenFound;
    4.     void Start ()
    5.     {
    6.     }
    7.    
    8.     void Update ()
    9.     {
    10.         if (!hasPlayerBeenFound){      
    11.             player = GameObject.Find ("Player");
    12.             guns = transform.Find ("Guns").gameObject;
    13.             if (player != null && guns != null)
    14.             {
    15.                 hasPlayerBeenFound = true;
    16.             }
    17.             else
    18.             {
    19.                 return;
    20.             }
    21.         }
    22.     }
    I added the return because you said these variables are needed to use this script further on.
     
    antonbergstig likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Put the scene managers code in awake then it will run first.
     
  4. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    Or this :)
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
  6. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    thank you :)
     
  7. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    yeah but i need it to be able to respaw (instantiate) and when it does the values get lost it needs to find
     
  8. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    i have try'd that doesnt work
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.