Search Unity

[SOLVED] SetActive prefab??

Discussion in 'Scripting' started by Davood_Kharmanzar, Jan 15, 2019.

  1. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    hello,
    this is my code to set active prefab ...
    but SetActive(true) does not working!!

    Code (CSharp):
    1. void Start() {gameObject.SetActive(false);} //its working
    2. void Update()
    3. {
    4.         if(BLAH BLAH BLAH) gameObject.SetActive(true); //not working :[
    5. }
    6.  
    howto fix that?
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Since you disabled the object it will not recieve Update events anymore.
     
  3. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    thanks ...

    i rename prefab on Start() to "Portal-Gateway" ...

    void Start()
    {
    gameObject.name = "Portal-Gateway";
    gameObject.SetActive(false);
    }

    i'm using another update function (on another object) to SetActive(true) this prefab ...

    void Update()
    {
    if(Blah Blah Blah) GameObject.Find("Portal-Gateway").SetActive(true);
    }

    but got this error:
    Object reference not set to an instance of an object
     
    Last edited: Jan 15, 2019
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    From the docs: "This function only returns active GameObjects"

    Please use code tags.

    You're not using an other object, 'gameObject' points to the game object the script is attached to.
    What you want to do is store a reference to said object and use that.
     
  5. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    so ... howto assign tag in runtime? i mean un predefined tag ...
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I meant this, on the forum https://forum.unity.com/threads/using-code-tags-properly.143875/


    firstly, let's get the terminology correct, a Prefab is not an object is the scene, it's a Prefabricated "blue-print" for an object.

    if you have an object in the scene and you need to do stuff to it while it's inactive you need a reference to it.
    IE:
    Code (CSharp):
    1. GameObject prefab;
    2. GameObject instance;
    3.  
    4. void Start(){
    5. instance = (GameObject)Instantiate(prefab); //Spawn a copy of 'prefab' and store a reference to it.
    6. instance.SetActive(false); //turn off the instance
    7. }
    8.  
    9. void Update(){
    10. //access it via instance
    11. instance.blah;
    12. }
    If the script is attached to the object it will not run Update and other callbacks when inactive(also there won't be a need to instantiate it)
     
  7. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    thanks for all replies ...

    but i'm using this stupid solution and its working :D

    Code (CSharp):
    1. Vector3 Position;
    2.  
    3.     void Start()
    4.     {
    5.                Position = gameObject.transform.position;
    6.             gameObject.transform.position = new Vector3(0, -999999999, 0);
    7.     }
    8.  
    9.     void Update()
    10.     {        if(Blah Blah Blah) gameObject.transform.position = Position;
    11.     }