error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Transform'. An explicit conversion exists (are you missing a cast?) an iterator interface type - C#

hi
i am trying to Rock Dorpper and after 5 seconds wait, but I keep getting Error CS0266

public class RockDorpper : MonoBehaviour {
// Instantiates prefab when any rigid body enters the trigger.
// It preserves the prefab's original position and rotation.

	public Transform [] prefab;
	
	public int prefabLength;
	void Start(){
		prefabLength=prefab.Length;
			
	}
	IEnumerator OnTriggerEnter(){
		
	   Transform clone =Instantiate (prefab[Random.Range (0,prefabLength)]);
	   yield return new WaitForSeconds(5);
	   clone.rigidbody.isKinematic = true;

		
	}

help me plz

right! Thank you

2 Answers

2

Instantiate returns an Object, you need to cast Transform.

line: 14

Transform clone = (Transform)Instantiate (prefab[Random.Range (0,prefabLength)]);
// or if you want to check for null and not just throw an exception.
Transform clone = Instantiate (prefab[Random.Range (0,prefabLength)]) as Transform;

Nice follow up Hoeloe

Not sure about C#, but you could try

Instantiate(object) as Transform;

or make prefab and clone a gameobject array instead.

Also, looking at the code, I’m not sure it knows what it’s instantiating.