- Home /
How to make an object move with a script which isn't attached to it?
Hello!
I am trying to make a script spawn a prefab whenever the player presses space, facing the direction of the player. The object will then move in the direction of where the player was facing, at a speed of 10. This is the only working code I can figure out, every time I try making the object, which is not what the script is attached to, move, I get an error.
If anyone could show me what could work and why I would be extremely grateful! Thanks!
if (Input.GetKey(KeyCode.Space)) { Instantiate(LasarBeam, transform.position, transform.rotation);
}
Answer by Larry-Dietz · Dec 26, 2017 at 02:02 AM
Add a script to the LasarBeam prefab that moves the beam. Then once your object instantiates the beam, the beam handles it's own movement from there.
-Larry
Your answer
![](https://koobas.hobune.stream/wayback/20220612145223im_/https://answers.unity.com/themes/thub/images/avi.jpg)