Search Unity

Instantiate Prefab from array at Vector3 as a child GameObject

Discussion in 'Scripting' started by ParadoxSolutions, Oct 12, 2015.

  1. ParadoxSolutions

    ParadoxSolutions

    Joined:
    Jul 27, 2015
    Posts:
    325
    I am trying to instantiate a random prefab from an array at a the position of an existing gameobject and parent it to that gameobject. The problem is I get this error "Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)" But it all seems like it should work, I have probably searched every possible similar question with no results so I came here.

    Code:

    GameObject[] prefab;
    Vector3 coords;
    int index;
    GameObject randomPrefab;
    string parentName;

    //gets all prefabs in the "prefabFolder" and adds them to an array of GameObjects.
    void start()
    {
    prefab = Resources.LoadAll("prefabFolder") as GameObject[];
    populateScean();
    }

    //gets a random prefab from the array.
    public void getRandomPrefab()
    {
    index = Random.Range (0, prefab.Length);
    randomPrefab = prefab[index];
    }

    //populates the whole scean by populating each area (area is a terrain GameObject).
    public void populateScean()
    {
    getRandomPrefab();
    parentName = "name1";
    coords = GameObject.Find(parentName).transform.position;
    populateArea(randomPrefab, coords);

    getRandomPrefab();
    parentName = "name2";
    coords = GameObject.Find(parentName).transform.position;
    populateArea(randomPrefab, coords);

    //I do this a total of 8 times because I have 8 terrain gameObjects (areas I called them in this example) they act similarly to chunks.

    }

    //parents the random prefab at the coords of the area.
    public void populateArea(GameObject randomPrefab, Vector3 coords)
    {
    GameObject rPrefab = Instantiate (randomPrefab, coords, Quaternion.identity);
    rPrefab.transform.parent = gameObject.transform;
    }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    first, use the CODE tags to make your code more readable.

    As for your problem it exists in the 'populateArea' function here:

    Code (csharp):
    1.  
    2. //parents the random prefab at the coords of the area.
    3. public void populateArea(GameObject randomPrefab, Vector3 coords)
    4. {
    5.     GameObject rPrefab = Instantiate (randomPrefab, coords, Quaternion.identity); //RIGHT HERE
    6.     rPrefab.transform.parent = gameObject.transform;
    7. }
    8.  
    the 'Instantiate' function from the Unity API returns its result typed as 'UnityEngine.Object', because Instantiate can be used to create clones of all sorts of unity objects other than GameObject (yeah, I wish there were overrides... or generics... but there isn't).

    So you have to case it to the type you know/expect it to be:

    Code (csharp):
    1.  
    2. //parents the random prefab at the coords of the area.
    3. public void populateArea(GameObject randomPrefab, Vector3 coords)
    4. {
    5.     GameObject rPrefab = Instantiate (randomPrefab, coords, Quaternion.identity) as GameObject; //RIGHT HERE
    6.     rPrefab.transform.parent = gameObject.transform;
    7. }
    8.  
     
    ParadoxSolutions likes this.
  3. ParadoxSolutions

    ParadoxSolutions

    Joined:
    Jul 27, 2015
    Posts:
    325
    Thanks, that's weird but makes sense. I'm new to Instantiation I thought it was saying rPrefab was what was wrong; And i'll be sure to use the code tags next time, this was my first thread. Again thanks for the help.