• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Activating GameObjects one by one 2 Answers

Multiple Cars not working 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges