Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by navadeep2011 · Apr 22, 2013 at 09:07 AM · gameobjectrendererdisplayactive

how to make my gameobjects disable and enable?

Hi all, i have two game objects in my project. i want to show only one in the scene. i wrote code like this.

  using UnityEngine;
  using System.Collections;

  public class Moon : MonoBehaviour {
 
 public bool onoff;
 public Texture2D icon;
 public GameObject table;
 public GameObject moon;
 // Use this for initialization
 void Start () {
 onoff=false;
 }
 
 // Update is called once per frame
 void Update () {
 
      if (onoff == true)
        moon.active = true;
     table.active = false;
      if (onoff == false)
     table.active = true;
        moon.active = false;
 }
 void OnGUI () {
     GUI.Button (new Rect (10,10,100,50), icon);
     {
     onoff=true;
     }
     
 }

}

my intention is i want to show any one by default. if the user click that icon i want to show second one and first becomes turn off. if he again clicks i want first one. like that i want to show.

but i add and run this code by default i didn't get the any one. i add all the things in the inspector view.

please help me.

thank you

navadeep

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by BenouKat · Apr 22, 2013 at 09:19 AM

You use "if" without the braces, but there's 2 instructions.

A if without braces but must be followed by only one instruction. In :

  if (onoff == true)
        moon.active = true;
     table.active = false;
      if (onoff == false)
     table.active = true;
        moon.active = false;

Your "table.active = false" and "moon.active = false" are out the if and will be called every frame. Try with braces.

Other things : You button is not correct. Try with this :

  if(GUI.Button (new Rect (10,10,100,50), icon))
     {
     onoff=true;
     }

The GUI.Button must be into an if statement. Because GUI.Button return a boolean which indicate if yes or no you push the button. So i guess for your problem :

table active is always false cause out the if. onoff = true in OnGUI is automaticly called because no if statement. so on the update, moon.active = true is called, but moon.active = false is out the if, so neither table nor moon will be active.

Tada.

PS : Prog tips, you don't need to write "if(onoff == true)", just "if(onoff)". Idem for negative comparaison "if(onoff == false)" becomes "if(!onoff)". :)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image navadeep2011 · Apr 22, 2013 at 09:32 AM 0
Share

Benou$$anonymous$$at,

ur Awesome.

I changed my code is like this

  using UnityEngine;
  using System.Collections;

   public class $$anonymous$$oon : $$anonymous$$onoBehaviour {
 
 public bool onoff;
 public Texture2D icon;
 public GameObject table;
 public GameObject moon;
 // Use this for initialization
 void Start () {
 onoff=false;
 }
 
 // Update is called once per frame
 void Update () {
 
      if (onoff == true)
     {
        moon.active = true;
     table.active = false;
     }
    else  if (onoff == false)
     {
     table.active = true;
        moon.active = false;
     }
 }
 void OnGUI () {
     if(GUI.Button (new Rect (10,10,100,50), icon))
    {
     onoff=true;
    }    
 }
      }




but there is problem in that if i clicked the button that is ok. if i again clicked i didn't get table again. First time only it is working.

avatar image BenouKat · Apr 22, 2013 at 09:43 AM 0
Share

This is normal. Everytime you click on the button, you set onoff to true. So you can click 309829 times, it will be always set to true :)

Change "onoff = true" by "onoff = !onoff" on your button statement and you done.

Last tip : This problem is very very very basic progra$$anonymous$$g, don't hesitate to read some books or tutorials for learning some basics reflections in progra$$anonymous$$g :)

avatar image navadeep2011 · Apr 22, 2013 at 09:48 AM 0
Share
       if(GUI.Button (new Rect (Screen.width-(Screen.width/7),10,100,50), icon))
 {
 if(!onoff)
 onoff=true;
 else
 onoff=false;
 }    


i wrote like this it is working fine thanks for help

avatar image BenouKat · Apr 22, 2013 at 09:59 AM 1
Share

No problem ;) Don't forget to mark the issue as resolve !

avatar image
1

Answer by Sanky · Apr 22, 2013 at 09:24 AM

Dude there is have methods for hide object on any event from the scene gameObject.SetActive (boolean); //this will active or deactivate your object from the scene

gameObject.renderer.enabled = boolean; //this will enable/desable your object mesh

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I hide a GameObject without active=false? 3 Answers

How to disable gameobjects in code? 4 Answers

Child object of another GameObject invisible or inactive @ runtime 2 Answers

How to use if statement properly 1 Answer

Toggling a game object between an active and inactive state 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges