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

Toggle gamobject

Discussion in 'Scripting' started by Mr.badatscripting, Apr 8, 2011.

  1. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    Does anyone by any chance know how to toggle a game object. I am trying to toggle a lantern and i cant find out how
     
  2. razormax

    razormax

    Joined:
    Nov 2, 2010
    Posts:
    151
    What do u mean my toggle? Like making it visible/invisible just graphically or disabling it completely?
     
  3. tool55

    tool55

    Joined:
    Jul 10, 2009
    Posts:
    334
    Code (csharp):
    1. var lantern : Light;
    2.  
    3. private var toggle : boolean = true;
    4.  
    5. function Update
    6. {
    7. if (Input.GetKeyDown("t"))
    8. {
    9. toggle = !toggle;
    10. lantern.enabled = toggle;
    11. }
    12. }
    This will make the light component turn on and off if that's what you're looking for.
     
  4. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Yes I had to do this toggling of gameobject once and I think no way to reassign or toggle gameobject, I guess it is because the gameobject is created object and not a assigned variable....

    So the way I took is .... Create a Empty Object...and At the runtime Instantiated the gameobject prefab with a Texture that I want to create and made it as child of the emptyobject...When I want to Change the gameobject with another Texture ... I just disabled the child object and instantiated it again with a gameobject prefab (with another Texture) and made it as child of the empty object.... Now I could do the toggling of the gameobject...

    Hope,this is what you meant....



     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There are two issues here:

    Toggling the GameObject and Toggling the Light.

    The first thing you need to do is watch Stanly Kubrik's (not Stanley Kubrick) acclaimed movie "How I stopped worrying and learned to love the Unity3D scripting documentation". I am not sure if it was released in your neck of the woods, but it tells of a young apprentice who finally opened the scripting reference pages! ;)

    http://unity3d.com/support/documentation/ScriptReference/

    If you do a search for "Light" and "GameObject", you will get these two pages:
    http://unity3d.com/support/documentation/ScriptReference/Light.html
    http://unity3d.com/support/documentation/ScriptReference/GameObject.html

    If you look at the inspector of a Light, you will see that "Light" is a component on a GameObject. GameObjects are have value called "active". You can set the GameObject.active = true/false to turn it on and off. Components have a value called "enabled". You can set the Component.enabled = true/false to enable or disable the component.



    Tool55 is correct. This code will turn your component on and off.

    Code (csharp):
    1. var lantern : Light;
    2. private var toggle : boolean = true;
    3.  
    4. function Update {
    5.      if (Input.GetKeyDown("t")) {
    6.           toggle = !toggle;
    7.           lantern.enabled = toggle;
    8.      }
    9. }
    Just drag your GameObject with your Light Component attached into the exposed public variable "lantern" in the inspector and hit "Play". The "t" key will toggle the light.

    Dinesh Balu: I am a litte confused as to what you are suggesting. It seems a little over complicated. You can toggle GameObject.active as long as you have a valid reference to that GameObject. There are two way to do this. You can either declare a public variable: var myGameObject : GameObject (or public GameObject myGameObject in C#) and this will show in the inspector. Drag your chosen GameObject onto the shown slot in the inspector. Then in code you can simply use a similar bit of code as listed above: myGameObject.active = !myGameObject.active;

    This will have a lot of hassle.

    Changing textures on an object at Run-Time is a little more complicated, and you need to look into materials and renderers. These are also in the documentation, and should probably be in a different post as it's not related to enabling or activating Components or GameObjects.
     
  6. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    where did you get my toggle. but yes make it visible/invisible
     
  7. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    Thanks guys for answering my tab your a big help. I was also wondering how I would be able to toggle the gameobject with a GUI switch. I know how to make it toggle with buttons but not a switch can you ether help or post links on GUI tuts? Thank you.
     
  8. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    hahahah sorry guys for being a pain but I am trying to make a script that lets you go on to another level with OnTriggerEnter any help?
     
  9. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    All you really need is OnTriggerEnter() (which you already mentioned), and Application.LoadLevel(). If objects other than the player can enter the trigger, you may need to check the object tag or name as well.

    If that doesn't give you what you need, post the code you have so we can take a look.
     
  10. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    Well i know how to make the ontriggerenter but i dont know how to select only one trigger. If i have many Triggers i dont want all of them loading the next level
     
  11. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Only attach the 'load level' script to the trigger that you want to load the next level.