Hi
I created a prefab and i’m trying to make it appear when the game starts, here is the code i tried:
public GameObject ballPrefab;
Vector2 startPosition;
void Start ()
{
startPosition = new Vector2(4,1);
Instantiate(ballPrefab, startPosition, transform.rotation) ;
}
I also assigned a prefab in the inspector, but it never appears unless i drag it into the Hierarchy window.
Please, help !
This code just works as intended (well, startPosition should be Vector3 not Vector2, but anyway it’s automatically casting Vector2 to Vector3 with the Z set to 0). Are you sure that the script is enabled on the gameobject where you attached it in the scene?
Code to be executed must be attached to a GameObject actually in the scene. Code on prefabs in the asset folder will not run unless an instance of the prefab is created in the scene. For your purposes I would suggest an empty GameObject with this script attached.