Doubt about the Event Functions and the "Time" class.

Hi there!!
Straight to the point:
There are this two scripts:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	public float distancePerSecond;

	void Update () {
		transform.Translate(0, 0, distancePerSecond * Time.deltaTime);
	}
}

And this one:

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {
	public GameObject enemy;
	public GameObject player;
	
	void Update() {
		if (player.transform.position.z == 5.0f) 
		{
			Instantiate (enemy);
		}
		else if(player.transform.position.z == 10.0f)
		{
			Destroy(player);
		}
	}

}

the thing is that no matter what i do (With my limited knowledges) the Instantiate and destroy functions doesn’t work unless i don’t use the Time.deltatime … I would really like to know why!! D:
I have been testing a variety of combinations of Event functions but none of them seems to work!
Any tips?

2 Answers

2

You are using == against a float value, which is not always what you will expect.

You are expecting these to be 5.0 or 10.0, which may just worked by accident before you introduced deltaTime. Now though, the odds of the z value being one of those two numbers are extremely low.

Not sure what you are trying to accomplish, but you probably want something more like:

    if (player.transform.position.z >= 5.0f && player.transform.position.z < 10.0) 
     {
         Instantiate (enemy);
     }
     else if(player.transform.position.z >= 10.0f)
     {
         Destroy(player);
     }

Thanks @pako and @rageingnonsense I'll take a look at it!! =) And ... about what i was trying to do ... well, it is not that much xP I was simply Re-Checking the Scripting Overview on the Unity Manual! =) And after the creating and destroying gameobjects part ... i tried to make a little practice exercise! =) It is simple: If the "Player's" 'Z' value is higher than 5 then an enemy will spawn ... and if it is higher than 10 or any other number the player will be destroyed! ... .as i said ... it isn't anything for a game .... just practicing what i learned!! ^^

Well ... it worked perfectly!! ^^ Now i only have the problem that i need it to spawn only one time! xP That's why i was trying to do it more "Precise" hahahha

@rageingnonsense So the right thing to do will be to "Re-Spawn" the player at origin instead of destroying him, right? And to avoid all this mess .... Use a "OnTrigger - Function" instead? xP .... My head is literally bleeding right now hahahhaha

Thanks man!! ^^ You were/are .... i dunno xP (I'm a Spanish guy) really helpful!! :') (y)

@Bunny83 thanks for the clarifications. It makes real sense now. The book I had read to learn how to use Unity, was a good book, but it didn't explain very well this particular point, so I always felt that something was just not right with my way of thinking about this :-) Always eager to learn more :-) @Maenor sorry for the confusion.

You’re probably running into floating point imprecision, something that happens in computer programming because the compiler is translating binary numbers into base 10. Try using Mathf.Approximately instead.

I haven't looked for tutorials but if you post a question here on Unity Answers or in the forum with your desired effect or description of the information you need, people will surely get you started.