- Home /
Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'
using UnityEngine; using System.Collections;
public class NewBehaviourScript : MonoBehaviour { public GameObject aaa; public GameObject[] aa = new GameObject[5]; public Vector3[] haha = new Vector3[] { new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(2,0,0), new Vector3(4,0,0) };
void Start ()
{
for(int i = 0; i < 4; i++)
aa[i] = Instantiate(aaa, haha[i], transform.rotation);
}
}
Unity told me that :
Assets/NewBehaviourScript.cs(13,25): error CS0266: Cannot implicitly convert type "UnityEngine.Object" to "UnityEngine.GameObject". An explicit conversion exists (are you missing a cast?)
I don't know what to do &=(
Answer by fireDude67 · Jan 08, 2011 at 03:45 AM
The function Instantiate
returns an Object
. So your code for instantiating should be:
aa[i]=Instantiate(aaa,haha[i],transform.rotation) as GameObject;
or
aa[i]=(GameObject)Instantiate(aaa,haha[i],transform.rotation);
I like the first way better, but they are both the same
Not exactly the same, but the desired result is the same. http://answers.unity3d.com/questions/20328/what-is-the-difference-between-prefix-casting-typevariable-and-as-casting
Your answer
Follow this Question
Related Questions
instantiate object 1 Answer
Getting list music array 1 Answer
instantiate a Rigidbody not work for me? 2 Answers
Getting a button to run a function 2 Answers
convert type UnityEngine.Object to UnityEngine.AudioClip 1 Answer