- Home /
why wont the instance of the object move after its been instantiated.
Hello, I was just wondering if you could proof read this script and tell me why my instance of an object wont move after its been instantated. It is supposed to spawn a clone of a cube in a specified position every second and move in the forward direction. Afterwards it will delete after a specified time. It's doing everything right except it's not moving forward.I have all the values and an object set in the inspector. I have this script on an empty game object and I have a gameobject that it will make instances of. Thanks. Script:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public GameObject enemy;
public float SpawnSpeed = 1f;
public float destroyTime;
public float MoveSpeed;
Transform MyTransform;
void Start () {
MyTransform = transform;
InvokeRepeating ("Spawn", SpawnSpeed, SpawnSpeed);
}
void Update () {
MyTransform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
}
void Spawn(){
Vector3 spawnLocation = new Vector3 (Random.Range (-5, 5), 1f, 0f);
GameObject clone = (GameObject)Instantiate (enemy, spawnLocation, Quaternion.identity);
Destroy (clone,destroyTime);
}
}
This code is moving the object with the script.
The enemy may have a script for movement but this is not affecting it.
How about you create another sccript that just moves the enemy forward. this script should just instantiate the enemy. enemy instantiated from a prefab with a script attached to it. after instantiating the enemy will run itsself and destroy itself.
Answer by LostInCode404 · May 15, 2015 at 03:08 AM
I think you are moving the object which is spawning the enemy and not the enemy itself thats why the object the script attached to is moving by transform.translate. To actually move the enemy you have to write clone.transforn.translate below instantiate line and above the deatroy line. Hope it helps. Ask me if you have sone doubts
Nope, if he does that the object will only move that one time.
What he needs is create a new script, copy paste the Update from that script to the new script and remove it from this one.
Then the object will move.
Thanks guys, once I get on my computer I will be sure to try it.
Can you tell us the script you wrote . so i can probably guess the problem
using UnityEngine; using System.Collections;
public class othe : $$anonymous$$onoBehaviour {
Transform $$anonymous$$yTransform;
public float $$anonymous$$oveSpeed;
void Start () {
$$anonymous$$yTransform = transform;
}
// Update is called once per frame
void Update () {
$$anonymous$$yTransform.Translate (Vector3.forward * $$anonymous$$oveSpeed * Time.deltaTime);
}
}
Answer by Teh_ouj · May 15, 2015 at 01:33 PM
The code is improperly written. just take the part inside the update ,as @fafase said, and put it in the update of a seperate script. Also put the destroy function in the start of the new script. attach that script to enemy and make the whole thing as a prefab. Prefab it by dragging your enemy from hierarchy to the project section. then its a prefab. This script should only intantiate. here is what it should look like:
SpawnEnemyScript:
public GameObject enemy;
public float SpawnSpeed = 1f;
void Start () {
InvokeRepeating ("Spawn", SpawnSpeed, SpawnSpeed);
}
void Spawn(){
Vector3 spawnLocation = new Vector3 (Random.Range (-5, 5), 1f, 0f);
GameObject clone = (GameObject)Instantiate (enemy, spawnLocation, Quaternion.identity);
}
EnemyScript:
public float MoveSpeed;
public float destroyTime;
void Start(){
Destroy(this.gameobject);
} void Update(){
this.tranform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
}//this.transform means the transform of who ever is attached to this script
So now just drag your enemy prefab to the EnemySpawnScript's enemy slot.
Hope this helps...
Thank you very much it works great, I just have one question. How can I make it so after a certain amount of time a clone will delete but just that clone that reached that time and not delete the whole prefab?
What do you mean? you have multiple clones and certain clones will destroy at different times? because the prefab is never destroyed. so all clones will die at the time specified.
Ah just put the time in the destroy statement... Destroy(Object obj, float t = 0.0F);
I want to have it so the cubes are spawning every amount of time. But every time a cube (clone) reaches the specified time it will destroy that clone. Right now once a clone reaches 20 seconds it destroys the prefab.
Your answer

Follow this Question
Related Questions
How to randomly spawn three non repeating gameobjects from an array? 2 Answers
Instatiating a prefab in a random position 0 Answers
Spawn Random Enemy 3 Answers
instantiating vertically 2 Answers