• 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
Question by Pendragon420 · Oct 20, 2014 at 06:15 AM · gameobjectsetactiveboolc

how do i use gameObject.setactive with a bool?(my codes not working!)

**Basically i have a bool called switch that i'm getting from another script and i want a platform to be set as inactive when the bool is true, and active when the bool is false. i can get it to go inactive but i cant get it back to active. hope you guys can help help me out its doing my head in! i had a similar thing working with layer collisions but i think this is the better way to go :)

here's my code: **

 using UnityEngine;
 using System.Collections;
 
 public class SwitchBlue : MonoBehaviour {
 
     public GameObject player;
     private ActionManager Manager;
 
 
     // Use this for initialization
     void Start () {
         Manager = player.GetComponent<ActionManager>();
 
     
     }
     
     // Update is called once per frame
     void Update () { 
         if (Manager.Switch == false) 
         {
             gameObject.SetActive (true);
         }
 
         else if (Manager.Switch == true)
         
         {
             gameObject.SetActive(false);
 
         } 
        
     
 
 }
 }

thanks guys!

Comment

People who like this

0 Show 2
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 Pendragon420 · Oct 20, 2014 at 06:17 AM 0
Share

so simple! thank you greatly master

avatar image HarshadK · Oct 20, 2014 at 06:45 AM 0
Share

I've converted my comment to answer. You can select it as an answer if you want. :-)

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by HarshadK · Oct 20, 2014 at 06:44 AM

No script on game object will execute after it is inactive.

So this part of your script

 if (Manager.Switch == false) 
 {
              gameObject.SetActive (true);
 }

will never execute since your game object is inactive.

You need to do it from an another script where you get the reference to your game object and then active or inactive it from there.

Comment

People who like this

0 Show 0 · 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

Answer by RedDevil · Oct 20, 2014 at 06:45 AM

Once you disable the item the script is on, you cannot enable it again because there is no call to enable it since the item is disabled.You need the script that enables and disables the object on a different object.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Simple GameObject Hide/Unhide with bool!!! 0 Answers

Scriptural Child Implementation 2 Answers

Is it possible to determine if OnDisable() is being called from parent.SetActive( false ) or self? 2 Answers

"Mesh 'NormalCurveRendererMesh': abnormal mesh bounds..." error appears on editor... 0 Answers

Script is losing references once gameobjects get disabled. How can I fix this? 0 Answers


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