Prefab with same scripts do the same thing
In my game 2D I instantiate 2 prefabs that contain the same simple script that basically moves the object back and forward within the x axis, using his current x value. When the objects get instantiated, they get different x coordinates at the start, so let say prefab1 starts with position 100 and prefab2 starts with position 400 (The instantiation is done using a script in the GM). The issue is that as soon as the scene starts both prefabs begins its movement in the same position as if they ignored the instantiated starting x coordinate.
See the code for the instantiation:
public GameObject wallAutoPrefab;
void Fase23(){
// Constantes - Posições
alvoPos1 = new Vector2(0,384);
playerPos1 = new Vector2(0,-512);
wallPos1 = new Vector2(-192,192);
wallPos2 = new Vector2(192,-192);
// Constantes - Escalas
wallScale1 = new Vector3(32,1,1);
wallScale2 = new Vector3(32,1,1);
// Instancia Objetos
GameObjectUtil.Instantiate(alvoPrefab, alvoPos1, consRot, consScale);
GameObjectUtil.Instantiate(playerPrefab, playerPos1, consRot, consScale);
GameObjectUtil.Instantiate(wallAutoPrefab,wallPos1,consRot,wallScale1);
GameObjectUtil.Instantiate(wallAutoPrefab,wallPos2,consRot,wallScale2);
}
See the code for the automovement that is attached in the wallAutoPrefab prefab:
public class AutoMove : MonoBehaviour
{
private float min;
private float max;
private float t;
private bool inverter = false;
void Start(){
if (transform.position.x > 0) {
min = transform.position.x;
max = min * -1;
} else {
max = transform.position.x;
min = max * -1;
}
}
void Update()
{
if (inverter == false) {
transform.position = new Vector3 (Mathf.Lerp (min, max, t), transform.position.y, transform.position.z);
t += 1.0f * Time.deltaTime;
if (transform.position.x == max) {
inverter = true;
t = 0.0f;
}
} else if (inverter == true) {
transform.position = new Vector3 (Mathf.Lerp (max, min, t), transform.position.y, transform.position.z);
t += 1.0f * Time.deltaTime;
if (transform.position.x == min) {
inverter = false;
t = 0.0f;
}
}
}
}
I tried everything, using foreach, using this code in the GM but I could not make it work. Please help me out here!! Thanks!
Your answer
Follow this Question
Related Questions
Spawn new character after animation 1 Answer
GameObject positions not lining up 0 Answers
Can't move instantiated object. 1 Answer
How to instantiate a prefab infront of First Person Controller 1 Answer
Player Prefab scale automatically goes to 1 x 1 x 1 whereas I have set it to 0.48 x 0.48 x 0.48 1 Answer