- Home /
Question by
sam · Oct 14, 2010 at 04:32 AM ·
errorsyntax-errorcastcs0266
instantiate object
Im trying to instantiate an object from my character however i have an error :Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.Rigidbody'. An explicit conversion exists (are you missing a cast?
using UnityEngine; using System.Collections;
public class Trajectory : MonoBehaviour
{
public Rigidbody projectile;
void Update()
{
if (Input.GetKeyDown("space")) {
Rigidbody clone;
**clone = Instantiate(projectile, transform.position, transform.rotation);**
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
Debug.Log("Hello");
}
}
}
Comment
Answer by · Oct 14, 2010 at 05:21 AM
Instantiate returns an Object. You need to explicitly cast it to a Rigidbody (as 'clone' is not an Object).
clone = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);