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 Evan Stallings · Jul 29, 2013 at 05:38 PM · c#objectenable

Can't set object to true (C#!)

Hi. I am trying to make a "blink" effect on my custom first person controller character, and I have it almost working. I have a countdown timer and you are automatically blinking when the game starts... it waits 3 seconds and unblinks. Now, I am trying to prototype continuos-blinking, with the click of the mouse button (it will all eventually be random, I know how to do that). But, I am having some issues with my script. Here it is:

using UnityEngine; using System.Collections; public class Blink : MonoBehaviour { public float timeLeft = 10.0f; void Update () { timeLeft -= Time.deltaTime; if (timeLeft < 1.0f) { gameObject.active = false; } if (Input.GetButtonDown ("Fire1")) { gameObject.active = true; } } }

The first part works perfectly fine. I start the game with the eyes closed, and 3 seconds later they open up (the cubes around the player are un-activated). But the second part simply does not work. No errors, just does not work.

(ALSO: I have tried putting the second block of code I am having trouble with (the second if statement) in another section like Awake, and still get the same results.)

I have tried different ways of writing it, such as gameObject.active = true/false;, GameObject.SetActive... etc. etc.

Please, any help would be greatly appreciated.

Thanks!

Comment
Add comment · 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 SolidSnake · Jul 29, 2013 at 05:40 PM 0
Share

is the script attached to the gameobject you are disabling? if so then your script will not be called anymore once you set active to false

avatar image Evan Stallings · Jul 29, 2013 at 05:53 PM 0
Share

Ah, thanks. I solved it but that just helps make since of it all.

1 Reply

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

Answer by Lovrenc · Jul 29, 2013 at 05:42 PM

This has nothing to do with c#

 gameObject.active = false;

Here you set your object into inactive state. Objects scripts are NOT CALLEED ANYMORE. Therefore

 if (Input.GetButtonDown ("Fire1")) {
 
    gameObject.active = true;
 
 }

can never be executed. Script is NOT CALLED IN THE LOOP ANYMORE.

What you could do is

  • use a simple variable for your active/inactive state and execute code according to it

  • activate this object from script that is beeing called (like gameState script or w/e)

For instance:

 float timeBetweenBlinks = 10f;
 float timeSinceLastBlink = 0f;
 
 void Update() {
   timeSinceLastBlink += Time.deltaTime;
   if(timeSinceLastBlink > timeBetweenBlinks) {
     Blink();
     timeSinceLastBlink = 0f;
 
   }
 }


Or even better, lern and use Coroutines

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 Evan Stallings · Jul 29, 2013 at 05:52 PM 0
Share

Thank you! I actually got it to work about a $$anonymous$$ute ago, came to check here for other ideas.... the way you are describing is much better than what I just did! :D Thanks again.

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

17 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

Related Questions

What should I change in this instantiating a GameObject in a script using Unity? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

panel hide and show in NGUI 1 Answer

enable/disable object in heirachy from another object 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