Search Unity

Disabled an instantiate object?

Discussion in 'Scripting' started by slw825, Oct 3, 2013.

  1. slw825

    slw825

    Joined:
    Mar 23, 2013
    Posts:
    10
    Hey guys I was wondering is that possible to disabled an instantiate object?

    I instantiate a health bar in the game which is a guiTexture, and I want to disabled it when there is something between the camera and the object.
    It means that when player can't see that object, the health bar of the object won't come out.

    But is that even possible? THX~ :)
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Why don't you just do it? Do you think it might bite?
     
  3. Marsupilami

    Marsupilami

    Joined:
    Sep 22, 2013
    Posts:
    21
    Code (csharp):
    1.  
    2. private GUITexture storedGUITexture;
    3.  
    4. storedGUITexture = Instantiate (prefab, position, Quaternion.identity) as GUITexture;
    5.  
    Make a place to store the texture reference then when it is created associate it with that storage and use that storage to reference it later to disable and enable it.
     
  4. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    look up on the reference page for Physics.Ray, and var object = instantiate(sda);
     
  5. slw825

    slw825

    Joined:
    Mar 23, 2013
    Posts:
    10
    Uh....actually I have test it so many times.

    Here's my code:


    But I mean once when I instantiate guiTexture, it won't be disabled unless I destroy it.
    So that's where I stuck....
     
  6. slw825

    slw825

    Joined:
    Mar 23, 2013
    Posts:
    10
    My reply is down there but a little dumb Q : how to post code like what you do in the reply? haha :p
    This is just the first time I'm asking a question here.
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    How about
    myHB.guiTexture = null;?

    or myHB.guiTexture.enabled = false;

    Also, objects aren't disabled, they are set inactive.
     
    Last edited: Oct 3, 2013
  8. slw825

    slw825

    Joined:
    Mar 23, 2013
    Posts:
    10

    Wow, the second one does work!!!
    Thank you for your big help!! :D