- Home /
What the heck is wrong with this script?!
OK, I don't have time to solve this right now as I have to head to a meeting. If anyone here could figure out what the heck is wrong with this script, I'd GREATLY appreciate it. I'm trying to make it so when a gameObj passes a certain xcoord, a prefab of that obj is instantiated and the original obj moves back to an earlier xcoord. Take a look at the script below.
using UnityEngine;
using System.Collections;
public class SpawnManager : MonoBehaviour {
private Transform pusher;
public float minSpeed;
public float maxSpeed;
public float currentSpeed;
float x,y,z;
Vector3 xyzCombine;
private int negative33val = -33;
public int enemyCount;
public GameObject pusherobj;
// Use this for initialization
void Start () {
pusher = transform;
currentSpeed = Random.Range (minSpeed, maxSpeed);
z = 0;
x = 120;
y = Random.Range (-24, 24);
pusher.position = new Vector3 (x, y, z);
xyzCombine = new Vector3 (x, y, z);
}
// Update is called once per frame
void Update () {
pusher.Translate (Vector3.left * currentSpeed * Time.deltaTime);
if (pusher.position.x == -33) {
Instantiate(pusher, xyzCombine, Quaternion.identity);
enemyCount += 1;
pusher.position = new Vector3(x,y,z);
}
}
}
OK, honestly. What is wrong with that? Whenever it passes -33, nothing happens. I don't have a clue nor the time to look this over. Could anyone help?
Answer by AndyMartin458 · Jun 26, 2014 at 09:59 PM
Because you are translating the position, it is not guaranteed that the .x will ever exactly equal -33. You probably want this.
if (pusher.position.x <= -33) //less than or equal to
{
Instantiate(pusher, xyzCombine, Quaternion.identity);
enemyCount += 1;
pusher.position = new Vector3(x,y,z);
}
Thanks. What a simple answer. I overlooked such a silly thing :/
Your answer
Follow this Question
Related Questions
Instatiating a prefab in a random position 0 Answers
If gameobject moves do this 1 Answer
Object instantiates on top of the previous object 2 Answers
C# the position of the object 1 Answer
Instantiating an Object doesn't use the given position. 1 Answer