Instantiated Prefab not Moving
Hello, I'm quite new to C# and Unity and I've been trying to solve this issue for a while. I want my instantiated prefab to move once it loads into the scene, but despite the object loading in it stays static. I've tried moving my enemyMove() into Update() and FixedUpdate() but neither seem to work.
void Awake()
{
rigidbody2DComponent = enemyPrefab.GetComponent<Rigidbody2D>();
initialYPosition = transform.position.y;
}
void Start()
{
enemyObject = Instantiate(enemyPrefab, enemyInitialPosition.position, transform.rotation);
enemyObject.name = "Enemy";
enemyObject.transform.parent = transform;
enemyMove();
}
void enemyMove()
{
speed = Random.Range(-10, -20);
rigidbody2DComponent.AddForce(transform.up * speed, ForceMode2D.Force);
//keep track of the old x position
initialXPosition = transform.position.x;
//store the new x position
newXPosition = initialXPosition;
//new x position cannot be the same as the old x position
while (Mathf.Abs(newXPosition - initialXPosition) < 1)
{
newXPosition = Random.Range(-6f, 6f);
}
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" || other.tag == "resetWall")
{
enemyMove();
//instantiate a new enemy object everytime it hits player or the bottom wall
newEnemyObject = (GameObject) GameObject.Instantiate(enemyObject, new Vector2(newXPosition, initialYPosition), transform.rotation);
//Without changing the name, the original name will get a bunch of
//(clone) added to it as it respawns
newEnemyObject.name = "newEnemy";
//Destroy the old enemy
Destroy(this.gameObject);
}
}
Answer by ray2yar · Jan 20, 2019 at 02:03 PM
It appears you're grabbing the rigidbody of a prefab before instantiating it.... Delete your Awake code. To grab your rigidbody, in START after the object is instantiated, write this:
rigidbody2DComponent = enemyObject.GetComponent<Rigidbody2D>();
Thank you for replying, I tried adding that after instantiating but it still doesn't work. $$anonymous$$y prefab still loads in, but it still doesn't move.
void Start()
{
enemyObject = Instantiate(enemyPrefab, enemyInitialPosition.position, transform.rotation);
enemyObject.name = "Enemy";
rigidbody2DComponent = enemyObject.GetComponent<Rigidbody2D>();
initialYPosition = transform.position.y;
enemyObject.transform.parent = transform;
enemy$$anonymous$$ove();
}
Looking closer at your code I'm confused as to what function it's supposed to provide. Is this script on the enemyPrefab? (It can't instantiate itself). I thought this was like a "master" script on a separate gameobject, but I see you destroy it on a collision (which, would this even detect a collision if it's not on the enemy). So, I guess to answer your question it would help to know what you expect this script to be doing. Here's how I would approach narrowing down your problem....
First, I would comment out everything except your instantiate code and your movement code
Next, I would check the scene view and inspector to see what is going on with your enemy after it's instantiated.
Third, I would reconsider the structure of the code... I would put on the enemy only things that have to do with its movement. I would have a master gameobject responsible for spawning new enemies.
I realized that the script isn't on the enemyPrefab. I moved the code for movement into a seperate script and attached that to my enemyPrefab. It's moving now, but now the triggers aren't working and my instantiated object doesn't get destroyed it just keeps moving.
Here's the new $$anonymous$$ovement script for the prefab
void Awake()
{
rigidbody2DComponent = GetComponent<Rigidbody2D>();
}
private void Update()
{
$$anonymous$$ove();
}
void $$anonymous$$ove()
{
speed = Random.Range(-10, -20);
rigidbody2DComponent.velocity = new Vector2(0, speed);
}
Here's the changed script
void Awake()
{
rigidbody2DComponent = enemyPrefab.GetComponent<Rigidbody2D>();
initialYPosition = transform.position.y;
}
void Start()
{
enemyObject = Instantiate(enemyPrefab, enemyInitialPosition.position, transform.rotation);
enemyObject.name = "Enemy";
enemyObject.tag = "Enemy";
enemyObject.transform.parent = transform;
enemy$$anonymous$$ove();
}
void enemy$$anonymous$$ove()
{
//keep track of the old x position
initialXPosition = transform.position.x;
//store the new x position
newXPosition = initialXPosition;
//new x position cannot be the same as the old x position
while ($$anonymous$$athf.Abs(newXPosition - initialXPosition) < 1)
{
newXPosition = Random.Range(-6f, 6f);
}
transform.position = new Vector3(newXPosition, initialYPosition, transform.position.z);
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" || other.tag == "resetWall")
{
enemy$$anonymous$$ove();
//instantiate a new enemy object everytime it hits player or the bottom wall
newEnemyObject = (GameObject) GameObject.Instantiate(enemyObject, new Vector2(newXPosition, initialYPosition), transform.rotation);
//Without changing the name, the original name will get a bunch of
//(clone) added to it as it respawns
newEnemyObject.name = "newEnemy";
newEnemyObject.tag = "Enemy";
//Destroy the old enemy
Destroy(this.gameObject);
}
}
Your answer
Follow this Question
Related Questions
Character movement 0 Answers
Implementing a "flyer" game in with the old CardBoard Google sdk 2 Answers
Make Ball roll without imput 0 Answers
Hi, got a problem with my vertical axis. 0 Answers
CharacterController.Move isn't setting collision flags 1 Answer