Instantiation and Prefab control

So, i was working with some prefabs and got curious about something.
Is it possible to move a instantiated prefab (using transform.Translate for example) directly in the script it is instantiated?
Or do i really need a script just for it?

Just to give some context i instantiate a “laser” from the player script:

 if (Input.GetKeyDown(KeyCode.Space))
        {           
            Instantiate(_laserPrefab, transform.position + new Vector3(0, 1, 0), Quaternion.identity);
        }

but i need to make a separated script to create it’s behavior, i was trying to do it directly in the player script but couldn’t do it

Transform NewObject = Instantiate(_laserPrefab, transform.position + new Vector3(0, 1, 0), Quaternion.identity) as transform. i think

Gives an error. Cannot convert type GameObject to Transform

did u put: as Transform; after it?
if not u can use Gameobject instead of transform and get the transform from the gameobject so u can move it.

You can save instance into a variable and do whatever you need.

var instance = Instantiate(prefab, ...);
instance.transform.Translate(...);
2 Likes