In script, I instantiate a NavMeshAgent. For some reason, it’s impossible to cast the Object Instantiate creates to a GameObject, and therefore to do to get the NavMeshAgent properties. Here’s my code:
public class RandomLocations : MonoBehaviour {
public int count = 10;
public float distX = 20.0f;
public float distY = 20.0f;
public Transform prefab;
// Use this for initialization
void Start () {
for (int i = 0; i < count; i++){
Vector3 position = new Vector3(Random.Range(-distX, distX), 0.0f, Random.Range(-distY, distY));
Object inst = Instantiate(prefab, position, Quaternion.identity); //results in (inst == something that seems right)
print("Created agent no. " + count);
GameObject instance = inst as GameObject; //After this, (instance == null).
}
}
Anyone have any idea what I’m doing wrong? I’m completely new to Unity and C#, so feel free to state the obvious!