Moving a GameObject that was instantiated,Problem moving an instantiated Object.
So, I've been trying solutions for a dozen hours to no avail. What I want to achieve is to just move my instantiated GameObject to a fixed location when certain conditions are met. I have a big script that calls this lesser one that corresponds to the object. Script is fairly big, so I'll just copy the important details.
public class EnemyController : MonoBehaviour
{
Vector3 Position;
Vector3 HomePosition;
void Start()
{
Position = transform.position;
HomePosition = transform.position;
}
void Update()
{
EnemyMovement();
}
public void ChangePosition(Vector3 NewPos) //This is called from the other script to change the position
{
Position = NewPos;
Debug.Log("New position should be " + Position);
Debug.Log("Home position is " + HomePosition);
Debug.Log("The distance is " + Vector3.Distance(Position, HomePosition));
}
public void EnemyMovement()
{
float step = Time.deltaTime * 1.0f;
if (Vector3.Distance(Position, HomePosition) > 0)
{
transform.position = Vector3.MoveTowards(transform.position, Position, step);
Debug.Log("Should be moving here");
}
}
}
The code runs fine and does its thing, but when the time to move arrives, it doesn't do it. It feels as if the Update() (Which btw works fine at the start of the scene) just stops updating, but when I use a Debug.Log on the Update is never stops. I don't understand what I'm doing wrong.
On the Big script I instantiate my object like so:
MainEnemy = (GameObject)Resources.Load("Enemy/" + EnemyStatic.EnemyCode, typeof(GameObject));
FirstStation.SetActive(true);
Instantiate(MainEnemy, EnemyStation1);
This creates a copy of a new GameObject, whith its own code and everything. Note that when in the object script I put the Vector3.MoveTowards on the Update Method it works instantly, it is when I change the Position that it doesn't work. My debugs on the ChangePosition() give me the expected anwers, but still, not working.
I'm fairly new to unity, so answers for a 10yo are appreciated!
Your answer
Follow this Question
Related Questions
Invisible Prefab spawning at location of Player before being instantiated 1 Answer
Can't set transform.position AND constantforce.force? 1 Answer
Get a Insantiate particle system follow a GameObject 1 Answer
Move Gameobject towards/away from position based on the proximity of 2 other objects 0 Answers
Pickup instantiates from a projectile, but not where it is supposed to instantiate. 0 Answers