- Home /
Requesting help with Time.time & Instantiate script.
Greetings
So I want to instantiate an object after a certain time (4 seconds) and the code runs fine, no errors appear and I can execute the game but no object instantiates after the 4 seconds? Here's my code:
using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public GameObject EnemyFab; float gameClock; void Update () { gameClock = Time.time; if (gameClock == 4.0f) { Debug.Log("yo"); Vector3 position = new Vector3(Random.Range(-5, 5), 11, 0); Instantiate(EnemyFab, position, Quaternion.identity); } } }
I know there is a couple of unnecessary things in there but I've been trying everything I could think of to make the Enemy Instantiate. By the way I am not getting any yo's in the debug. I'd be grateful for any help or suggestions!
EDIT: Also I had another question; So instead of destroying my object after it collides with an enemy I set renderer.enabled = false;
and then a couple of seconds later it reapers when I set renderer.enabled = true;
but all that time is isn't rendered its still physically has a collier and a transform and can still collide with objects. So I would prefer a ?Function?Code?Method?(don't even know what to call it :| ) instead of renderer.enabled
that will physically move it or disable it for a certain amount of time?
Thank you very much for your time :)
$$anonymous$$aybe try gameClock >= 4.0f.... The time might not become exactly 4.0
Already tried that and after the first 4 seconds of the game its spawned a new enemy on the screen for every frame update. Vice versa does the same just for the first 4 seconds of the game. Thanks for the help :)
You could have
bool enemySpawned = false;
if(gameClock >= 4.0f && !enemySpawned) {
enemySpawned = true; //instantiation code.... }
$$anonymous$$erry xmas
you cannot compare two floating points.Use $$anonymous$$athf Approx or use > or < ins$$anonymous$$d of testing for equal results.
$$anonymous$$athf.Approximately http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Approximately.html
Answer by Tomer-Barkan · Dec 25, 2013 at 04:54 PM
The update code runs every frame. The time between frames varies depending on the performance of your code, graphics, PC speed, etc (what we call framerate is how many frames are run per second). So you don't know exactly how long between frames.
The chances that an update will run in exactly 4.0 seconds is small. You'll need to check it differently, you need to check if at least 4 seconds have passed.
Here's the "Unity" way of doing it:
public GameObject EnemyFab;
float spawnTime = 4;
void Update ()
{
if (spawnTime >= 0 && Time.time >= spawnTime)
{
Debug.Log("yo");
Vector3 position = new Vector3(Random.Range(-5, 5), 11, 0);
Instantiate(EnemyFab, position, Quaternion.identity);
spawnTime = -1;
}
}
public void SpawnInSeconds(float seconds) {
spawnTime = Time.time + seconds;
}
So basically what we're doing is keeping track of when the enemy should spawn, and putting it in spawnTime
. It starts with 4, so initially the enemy will be spawned after 4 seconds. Then every frame we checked whether the time to spawn has already passed, and if it has we will spawn the enemy, and set the time to spawn to a negative number (meaning it will never spawn until we change it). If you want to spawn in X seconds, simply call SpawnInSeconds(X), and it will set spawnTime to be X seconds from now.
About your second question, you can deactivate it with:
enemy.gameObject.SetActive(false);
and then enable it again with
enemy.gameObject.SetActive(true);
Thankyou very much! I understand how its unlikely for Time.time to exactly equal 4, how we check if 4 seconds has passed and how essentially we switch it off but defining spawnTime
as a number below zero (-1). I am very grateful as it works perfectly. But I have never dealt with 'calling' a function, I understand how the code works inside the function but I don't know how to call it.
And I tried Replacing renderer.enabled
with gameObject.SetActive
and it will remove when set to false so the player object cannot collide or interact or even move but it isn't being set back to true. Its weird because I put back in the renderer.enabled
to check if the If argument is at fault and it worked as it has been before. So the problem must lie with the gameObject.SetActive
. Here's my code: float timer = 0f; void Update () { if (Time.time - timer > 2) { gameObject.SetActive(true); } } void OnTriggerEnter(Collider collider) { if(collider.gameObject.CompareTag("Astroids") && Time.time - timer > 4) { playerLives -= 1; gameObject.SetActive(false); timer = Time.time; if (playerLives < 1) { Destroy(this.gameObject); } } if (collider.gameObject.CompareTag("Boss") && Time.time - timer > 4) { playerLives -= 1; gameObject.SetActive(false); timer = Time.time; if(playerLives < 1) { Destroy(this.gameObject); } } } }
Thanks for your help!
Calling a function is what you do when you set the gameObject to be active for example. If you'd want to call my function, simply use SpawnInSeconds(5);
at any part of your code for example. It's just a regular C# class function, method, or whatever you call it. Nothing special about it.
Disabling a game object using gameObject.SetActive()
, will do exactly that - disable the gameobject. That also means that the scripts of this gameobject will no longer execute, hence you cannot enable it from within it's own update code (that code is not running!)
What you could do is have an object that is always active, and it will take care of enabling and disabling other objects using coroutines:
public class Disabler : $$anonymous$$onoBehaviour {
public void Disable$$anonymous$$e(GameObject me, float time) {
StartCoroutine(Dsb(me, time));
}
public IEnumerator Dsb(GameObject obj, float time) {
Debug.Log("Deactivating");
obj.SetActive(false);
yield return new WaitForSeconds(time);
Debug.Log("Activating");
obj.SetActive(true);
}
}
Now all you need is to set a reference from your class to the object that has the disabler script, and call it when you want to disable your object:
public Disabler disabler;
void OnTriggerEnter(Collider collider)
{
if(collider.gameObject.CompareTag("Astroids") && Time.time - timer > 4)
{
playerLives -= 1;
timer = Time.time;
if (playerLives < 1)
{
Destroy(this.gameObject);
}
disabler.Disable$$anonymous$$e(gameObject, 2);
}
if (collider.gameObject.CompareTag("Boss") && Time.time - timer > 4)
{
playerLives -= 1;
timer = Time.time;
if(playerLives < 1)
{
Destroy(this.gameObject);
}
disabler.Disable$$anonymous$$e(gameObject, 2);
}
}
Don't forget to set the reference to the disabler object from the inspector.
Ahh I understand what calling a function is, silly me! Yes I see how the game object and therefore the script doesn't exist after gameObject.SetActive(false);
.
I'm afraid all those coroutines is over my head and out of my scope for my first attempt at scripting an entire project, and really what I've got render.enabled
works fine for what I need just has the side affect of still colliding with objects. So I think I'm gonna stick to the rule: If it isn't broken don't fix it.
Thank you very much for your help! much appreciated!
If you want you can also disable the collider and prevent it from colliding:
collider.enabled = false; // true to enable
I linked the coroutine tutorial, it could be useful later. Basically it's a method that can "yield" control, and then automatically resume after a couple of seconds (there are other uses as well).
collider.enabled
will work well with render.enabled
because I've decided that players still need the visual cue that they've lost a life.
Since this project is nearly wrapped up I think in my next project I'll learn to use coroutines because they seem very useful! Thanks for the link!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
How to whrite in c# the code: onMouseEnter Hide mesh 1 Answer