Search Unity

Use gameobject from other scene

Discussion in 'Scripting' started by Vindatra14, Aug 30, 2015.

  1. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    Excuse me, I want ask about this for example :
    I have 2 scene, scene start and scene main.
    at scene start I have button, if I press it I want to change the object at scene main. How I can use that object properties at scene main for button at scene start. Thanks
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
  3. Vindatra14

    Vindatra14

    Joined:
    May 8, 2015
    Posts:
    87
    which better, I mean for fast load. 1 and 2 have different and some + can u explain to me ??
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    DontDestroyOnLoad would be the easier method.

    In a script in your scene main, add this piece of code to any of your scripts :

    Code (CSharp):
    1. public GameObject myObject; //Assign the object here
    2.  
    3. void Awake(){
    4. DontDestroyOnLoad(myObject);
    5. }
    The object wont get destroyed when loading different scenes which means you can change it's properties at scene start and retain them when loading back to scene main.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860