• 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 Klasmic · Jul 15, 2017 at 08:49 AM · if-statementsui imagesetactivebool

SetActive not working

if have a script that is supposed to opens (sets active) an image and sets a bool to true when I click I and does the opposite when I click it again but It doesn't work, it doesn't change the bool or set SetActive to true I don't know what's wrong with it.

heres my script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Crafting : MonoBehaviour {
 
     public bool isOpen;
 
     public void Start(){
         gameObject.SetActive (false);
         isOpen = false;
     }
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.I) && !isOpen) {
             gameObject.SetActive (true);
             isOpen = true;
         }
             
         if (Input.GetKeyDown (KeyCode.I) && isOpen) {
             gameObject.SetActive (false);
             isOpen = false;
         }
     }
 }




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
0
Best Answer

Answer by OperationDogBird · Jul 15, 2017 at 11:54 AM

Once your GameObject is inactive, it will no longer run scripts.
You will need a different gameobject's script to set the state of this gameobject.

Comment
Add comment · Show 1 · 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
0

Answer by tanoshimi · Jul 15, 2017 at 08:52 AM

That's because in every frame in which I is pressed down, you're activating the object (first block of code) and then immediately deactivating it again (second block of code). You only want to execute one or the other, so use else.

      if (Input.GetKeyDown (KeyCode.I) && !isOpen) {
          gameObject.SetActive (true);
          isOpen = true;
      }
          
      else if (Input.GetKeyDown (KeyCode.I) && isOpen) {
          gameObject.SetActive (false);
          isOpen = false;
      }
Comment
Add comment · Show 1 · 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 Klasmic · Jul 15, 2017 at 09:06 AM 0
Share

@tanoshimi I tried this but it still did not work, hers my script now

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Crafting : $$anonymous$$onoBehaviour {
 
     public bool isOpen;
 
     public void Start(){
         gameObject.SetActive (false);
         isOpen = false;
     }
 
     void Update () {
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.I) && !isOpen) {
             gameObject.SetActive (true);
             isOpen = true;
         }
 
         else if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.I) && isOpen) {
             gameObject.SetActive (false);
             isOpen = false;
         }
     }
 }

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

70 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Both IF statements in a function are run and their conditions are ignored. 4 Answers

Reigns Game - Use Random.Range at Start() and a second time via another script and set GameObject as active 0 Answers

check if method is finished, bools not working 1 Answer

Mouse click IF not checking properly 0 Answers

text and image prefab not popping up when set.active 2 Answers

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