❔ How can I make an instantiated object move?
I'm trying to make an object spawned with the instantiate function move, but I can't get it to work. Can anyone help me?
public class Spawn_Cells : MonoBehaviour
{
public GameObject cell;
public float speed = 2;
public Vector3 cellDirection = new Vector3(1, 1, 0);
void Start()
{
Vector3 randomPosition = new Vector3(Random.Range(10f, -10f), Random.Range(4f, -4f), 0);
Instantiate(cell, randomPosition, Quaternion.identity);
}
void Update()
{
cell.transform.Translate(cellDirection * speed * Time.deltaTime);
}
}
8 Replies
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/cell is your prefab, you're not saving the instantiated object into a variable
you're trying to move the prefab object
rename
cell
to cellPrefab
, add another variable _cell
, when you instantiate save the result of calling instntiate into _cell
and then move this _cell
insteadThank you for the help
One question, what command do I use save the result of instantiating?
assignment
Ah ok thanks again
I only had time to try it now, and it worked, so I just wanted to say thank you so much!! I'm new to unity, and I've been stuck on this for a while now, such a relief to get it working
np
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.