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 darkmwar · Jun 04, 2014 at 04:19 AM · c#gameobjectdisablesetactiveactive

GameObject.setActive(bool) is not activating my gameObject

Hi, I am running in to a problem in unity ,when the player pressed this button, the gameObject "thing", (already dragged in to the prefab), would show up, and covers the whole screen to "punish" this player for pressing this button.

thing is just a sprite sheet. The point is so that the player cannot play the game temporarily.

Two Problems occurs,

one, eventhough the object .activeself returns true in the debug console, the sprite never showed up, and i debugged it by time.timescale=0 ( to pause the game), the object's box is unchecked in the inspector window.

two, I wrote the code in another version where the object was always non-active(unchecked from the start), and upon punish, set active, that worked, but I couldn't get it to go away ( thing.gameObject.setActive (false);) did not make it go away.

BUT ALSO, the buttons were still accessible, which is not what I want, since I want "thing" to cover the player's window to "punish" the player.

Can anyone help please? If there is another way to "punish" the player(disable all button inputs, and show a sprite), please let me know.

Part of the relevant codes:

 public gameObject thing;
 bool punish = false;

 void Start(){
       thing.gameObject.setActive(false);
 }

 private void OnGUI () {
    if (GUI.Button (nextB, ""){
         punish=true;
    }
 }

void Update(){

 if (punish) {

 float time = 0;
    while (time < 1) {    

         red.gameObject.SetActive(true);


         Debug.Log ("entered");  //Debug console shows enter
         Debug.Log (red.gameObject.activeSelf); //Debug console show true
        //Time.timeScale=0; to debug Object is unchecked at inspector window
        time = time + Time.deltaTime;
      }
    punish = false;
    red.gameObject.SetActive (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

1 Reply

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

Answer by vinod.kapoor · Jun 04, 2014 at 05:34 AM

 public GameObject thing;
 bool punish = false;
 private float time;
 void Start(){
       thing.gameObject.setActive(false);
       time = 0;
 }
  
 private void OnGUI () {
    if (GUI.Button (nextB, ""){
         punish=true;
         time = 0;
         red.gameObject.SetActive(true);// i dont know wheather it is should be  //red.gameObject.SetActive(true) or  thing.gameObject.SetActive(true)
    }
 }
 void Update(){
 
 if (punish) {
    if(time < 1) {   
  
       
        Debug.Log ("entered");  //Debug console shows enter
        time = time + Time.deltaTime;
      }   else{
    punish = false;
    red.gameObject.SetActive (false);// i dont know wheather it is should be     //red.gameObject.SetActive(true) or  thing.gameObject.SetActive(true)
  }

} }

hope this is the code u require ............ :)

Comment
Add comment · Show 5 · 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 Tarlius · Jun 04, 2014 at 06:23 AM 0
Share

This looks like the right answer, but doesn't explain the problem.

Your problem is the while loop. You will just add Time.deltaTime to time as many times as required in a single frame, then SetActive(false) straight after. This code correctly moves the time variable to the instance scope and updates in in Update.

If you want to use while, you will need to make a coroutine or make Update an IEnumerator and use yield null; inside the loop. Note that would could get rid of the loop by using something like yield return new WaitForSeconds(1f);

Generally it is better to use coroutines than Update to implement this kind of logic, but Update is much easier for beginners to get their head around.

avatar image fafase · Jun 04, 2014 at 07:30 AM 0
Share

And it still contains some errors I pointed out in my answer. But I don't see any while in his code...?

avatar image darkmwar · Jun 04, 2014 at 07:47 AM 0
Share

Thanks so much, that indeed did the trick, but the button is still infront of the "red" sprite.

avatar image Tarlius · Jun 04, 2014 at 09:07 AM 0
Share

Sorry. I meant the while in the original code! I didn't study vinod's code but it looks like it solves the main problem with the OPs code, but didn't explain what the problem was or how it was solved, so I just wanted to add a little detail to his response ;)

I don't see your answer fafase :s

avatar image vinod.kapoor · Sep 01, 2014 at 04:51 AM 0
Share

use if ((GUI.Button (nextB, "")&& punish){ ins$$anonymous$$d of if (GUI.Button (nextB, ""){

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

23 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

Related Questions

Calling setActive(false) on a disabled object? 3 Answers

How to active/deactive gameobject? 1 Answer

Activating GameObjects one by one 2 Answers

Alternatives to GameObject.SetActive(...) 2 Answers

Script only works onetime 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