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 Pa3trick · May 31, 2015 at 12:31 PM · c#gameobjectscript errortoggleactive

Script only works onetime

Hi,

I tried to create a script which toggles a GameObject (in this case an information text) on or off:

 using UnityEngine;
 using System.Collections;
 
 public class toggleInformation : MonoBehaviour {
 
     public GameObject ObjectToToggle;
     public bool active;
 
     void Start() {
         active = true;
     }
 
     public void toggle() {
         Debug.Log ("toggle");
         if (active = true) {
             ObjectToToggle.SetActive(false);
             active = false;
         } else if (active = false) {
             ObjectToToggle.SetActive(true);
             active = true;
         } else {
             Debug.Log ("Error: active neither true nor false");
         }
     }
 }
 

Unfortunately it only works to toggle off the GameObject (default: it is turned on when starting the game). Console shows only one "toggle" even though I pressed the button several times (button executes the script).

Any ideas? :-/

Thank you!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by error031 · May 31, 2015 at 12:41 PM

Is your script attached to ObjectToToggle? If it is, there is problem, because when ObjectToToggle is inactive, all scripts attached to it will be inactive too. My suggestion is to attach script to other gameObject.

Comment
Add comment · Show 7 · 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 Pa3trick · May 31, 2015 at 12:48 PM 0
Share

Thanks for your answer. The script is attached to an empty GameObject "_$$anonymous$$anager".

avatar image error031 · May 31, 2015 at 12:59 PM 0
Share

This script looks correct, can you post code in wich you call this toggle function?

avatar image Pa3trick · May 31, 2015 at 01:07 PM 0
Share

Here are two screenshots. I'm using a button to call the function. As I already mentioned, the script is attached to "_$$anonymous$$anager" (see 2nd screenshot). Note: I got the same problem with another script (changeColor: changes the color of some GameObjects).

Thank you for supporting me!

alt text

alt text

button.png (23.6 kB)
manager.png (102.9 kB)
avatar image error031 · May 31, 2015 at 01:25 PM 1
Share

I am so blind, i didn't saw that you wrote if (active = true) ins$$anonymous$$d if (active == true) Thats the whole problem my friend xD

Sorry that took me so long to notice this xD

avatar image hexagonius · May 31, 2015 at 01:32 PM 1
Share

I just stumble across this and saw that you can reduce your code quite a lot:

      public void toggle() {
          Debug.Log ("toggle");
              ObjectToToggle.SetActive(active = !active);
      }
Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject.setActive(bool) is not activating my gameObject 1 Answer

How many times awake function is called when toggle on/off gameObjects with the same C# script 1 Answer

Use a toggle button to change a GameObjects visibility 2 Answers

how to know if GameObject is active without getting warning? 2 Answers

Trouble getting GUI phone to open/close 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