Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Activating an Inactive object

Discussion in 'Scripting' started by dtaylorl, Jun 20, 2007.

  1. dtaylorl

    dtaylorl

    Joined:
    Dec 21, 2006
    Posts:
    22
    I wanted to use the following code:
    Code (csharp):
    1. credits = GameObject.Find("CreditsButton");
    2. credits.active = false;
    to activate an inactive object, but then I discovered that GameObject.Find only works on active objects. How can I reference an inactive object?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I assume you mean "credits.active = true" to activate it. You can assign the object using the inspector:

    Code (csharp):
    1. var credits : GameObject;
    ...outside any function, and then drag'n'drop. Or you can keep the object active and turn it off at startup, and keep the reference around for later:

    Code (csharp):
    1. private var credits : GameObject;
    2.  
    3. function Start() {
    4.    credits = GameObject.Find("CreditsButton");
    5.    credits.active = false;
    6. }
    7.  
    8. function TurnOnCredits() {
    9.    credits.active = true;
    10. }
    --Eric
     
  3. CBot

    CBot

    Joined:
    Jan 25, 2008
    Posts:
    29
    I found another way to do this using the Find function. Make an active empty game object the parent of the gameObject you need to have deactive. Put your script code on the parent object. Now when you run your script call gameObject.SetActiveRecursively(true). Now your child is active just in time to do something.



    Code (csharp):
    1.  
    2. function Activate() {
    3.    gameObject.SetActiveRecursively(true); // activates the children
    4.    // do interesting stuff here
    5. }
    6.  
    It would still be nice though if we could find inactive objects like this post suggests.


    http://forum.unity3d.com/viewtopic.php?t=6073
     
    mjood-10 likes this.