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 Orloffyeah · Dec 28, 2012 at 05:25 AM · timerbooleanactive

Deactivate An Object After Certain Amount of Time

Hi, I am using a script in which your weapon is active if you pass through a certain zone, but I would like that you only have it available for a certain amount of time and then it would deactivate until you pass through that zone again.

I'm thinking of something like:

 static var minigunUnlocked = false;
 
 if (minigunUnlocked == true)
 time active = 30;
 
 if time active = 0;
 minigunUnlocked == false;

I think that something with that logic would work, but how could I actually implement it?

Comment
Add comment · Show 11
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 Fattie · Dec 28, 2012 at 07:01 AM 0
Share

simply use Invoke() - it's only a line of code

for full details, many essays on unityGE$$anonymous$$S.com

avatar image alexfeature · Dec 28, 2012 at 07:08 AM 0
Share

Great advice! You got any more gems like this one?

avatar image Fattie · Dec 28, 2012 at 07:12 AM 0
Share

not sure what you mean $$anonymous$$! but the site "unityGE$$anonymous$$S.com" definitely has lots of "gemmy" ideas, heh!

another huge tip is "make sure you know how to use GameObject.Find and GetComponent" - perhaps the two most useful functions.

avatar image Fattie · Dec 28, 2012 at 07:17 AM 0
Share

NOTE ... ORIO ...

Never, ever use static for any reason. $$anonymous$$any people who are new to Unity are fooled by this. Absolutely do not use it. (You can google or search here for many essays on why not to.)

It is a very specific thing for certain advanced system program$$anonymous$$g, and has absolutely nothing to do with "global variables" or the like.

For some reason, it's a real plague in unity that beginners see "static" mentioned and use it (it must appear in some common example code or something). Never use it (until years from now when you are doing certain advanced system program$$anonymous$$g).

If you want to use a variable xyz "from another script" simply go

GameObject.Find("Spaceship").GetComponent(OtherScript).xyz

again, this is explained in vast detail at the great beginner site, unityGE$$anonymous$$S.com

avatar image alexfeature · Dec 28, 2012 at 07:41 AM 0
Share

static is a construct that exists for a reason. Telling people not to use it because it's evil or something is not great advice. Want to help? Point them to a resource that explains what it does and why it exists. Like this > http://www.javacoffeebreak.com/faq/faq0010.html

Show more comments

4 Replies

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

Answer by alexfeature · Dec 28, 2012 at 06:49 AM

Hey Dude,

To do this you would first need to put this code in the Update() function of your script.

Then the logic is pretty much the same as you have it with the only addition being the timer countdown.

Something like this :

 // This will let you configure your gun script from the editor 
 public float minigunTimeout = 30F;

 // Private stuff to make the activation timer work
 private float _minigunTimeRemaining;
 private bool _minigunActive = false;
 
 private void Start()
 {
    // Init our time remaining
    _minigunTimeRemaining = minigunTimeout;
 }
 
 // Inside your zone trigger, perhaps OnTriggerEnter()
 {
  _minigunActive = true;
 }
 
 private void Update()
 {
 
  if(_minigunActive && _minigunTimeRemaining > 0)
  {
   // Reduce the remaining time by time passed since last update (frame)
   _minigunTimeRemaining -= Time.deltaTime;
  }
  else
  {
   // The gun is now disabled and the timer is reset
   _minigunTimeRemaining = minigunTimeout;
   _minigunActive = false;
  }
 
 }
 
 // If you have a Shoot function then perhaps you would do 

// this to check if the gun can actually shoot

 private void Shoot()
 {
 
  if(!_minigunActive)
  {
   return;
  }
 
  // Shoot code here
 
 }


Everything is in C# because i cant remember the syntax for JS sorry :D

Alex

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

Answer by AndrewHerrera1998 · Apr 20, 2019 at 01:17 AM

easy, use Invoke and specify the time in secs:

 Invoke("HideShowGameobject", 5);

create a new method called HideShowGameobject:

 void HideShowGameobject()
     {
              if (objectToHide.active)
                    objectToHide.SetActive(false);
              else 
                   objectToHide.SetActive(true);
     }
Comment
Add comment · 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 Maulik2208 · Dec 28, 2012 at 07:25 AM

useGameObject.active = true;when weapon is active then use WaitForSeconds(); which will allows you a timer and after that just do GameObject.active = false;

Comment
Add comment · 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 Orloffyeah · Dec 28, 2012 at 05:43 PM

Thanks to all of you, I read the article about static and I think I will start replaicing it for "Get.Component" :D, also thanks for the quick responses, for just a simple question. Hope you have a nice day.

Comment
Add comment · Show 2 · 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 alexfeature · Dec 29, 2012 at 07:16 AM 0
Share

Those Getxxx functions are slow and should be used carefully. Never use them in Update methods or methods that run very often.

It's ok to use it in Start or Awake though, in most cases. $$anonymous$$ake a private variable in the class and assign the reference you get from .GetComponent to that variable.

Then anywhere you are in the class you have access to your component by simply using the variable. It's fast and convenient.

avatar image Fattie · Dec 29, 2012 at 09:06 AM 0
Share

Orlo -- just as $$anonymous$$ explains. Of course you only use GetComponent ONCE at the "top of each file" so to speak.

So you'll have this:

  private var superBoss:BigScriptName;

and then in Awake() you will have this

  superBoss =
     GameObject.Find("spaceship").GetComponent(BigScriptName);

This is just exactly how you code in Unity. Every single script you will ever do, has, like 10 or 20 of those at the top of each script.

from then on in that script, just use "superBoss"

Example,

 superBoss.showExplosions();
 x = superBoss.height + 1.25;
 superBoss.createSpaceship( 3.25, "hot black");
 etc

Exactly as Orlo says, this is mentioned on every Unity documentation page (like if you look at "GetComponent" it says something like "only call this once in Awake, don't call it every time you need to refer to the script") and it's explained endlessly at unityGE$$anonymous$$S, on here, etc etc

It's worth noting that sometimes YOU DO HAVE TO use "GetComponent" on the fly (notable in collisions etc) and there's very little you can do about that - it's not a disaster. But as a matter of course do exactly what Orlo says, just use GameObject.Find up the top of the script inside your Awake() routine

Also just to be clear. Say you're just doing a trivial test routine or something or you're just experimenting with code. There's no problem typing GameObject.Find("spaceship").GetComponent(BigScriptName) every single time. You're not going to break anythign by doing that. But later in production code for the app store, sure, you would do it "up the top" so you can then just type superBoss in that script from then on.

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

13 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

Related Questions

activating game objects with a tag 1 Answer

Deactivate children with .active = false 2 Answers

switching the active boolean 1 Answer

Game Over Screen Problem 3 Answers

How Do I check if a Bool was True a few moments ago 2 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