Hey, I want to put an object in another object (physically, in editor) clone it and then move it forwards. Now, I know I can make it move forwards with something like:
transform.position = new Vector3 (0, 5f, 0, Space.Self);
(or something alike)
But how do I make a clone of an object and select it and make it execute that command?
use a prefab.
build the object you want in the scene(hierarchy tab) and drag it to some folder in your asset(project tab) folder.
then make a script similar to this:
public class Example
{
public float speed;
public GameObject objectToClone;
private GameObject clonedObject;
void Start()
{
clonedObject = (GameObject)Instantiate(objectToClone, transform.position, transform.rotation);
}
void Update()
{
if(clonedObject == null)
return;
clonedObject.transform.position += Vector3.forward*speed*Time.deltaTime;
}
}
and drag that prefab in the objectToClone slot in the inspector after you have put the script on a game object
Also, I tried to put entire ball (projectile) and prefab under camera, so the projectile goes in direction of which I’m facing, but it doesn’t work. I tried it by making Hierachy.
You are using the code tags incorrectly. If you want the new object to move as well, you should put the move code to a separate script and attach that to the prefab.