- Home /
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?
simply use Invoke() - it's only a line of code
for full details, many essays on unityGE$$anonymous$$S.com
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.
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
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
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
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);
}
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;
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.
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.
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
Follow this Question
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