zak666
1
Hi gang made a simple instantiate ridged body with a courntine but says:
error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.Rigidbody’. An explicit conversion exists (are you missing a cast?)
for both instances for my instatiations of ridged bodies.
using System.Collections;
public class PlayerFighter : MonoBehaviour {
public GameObject MachGunBullets;
public GameObject Missiles;
public int MissilesAmmount = 3;
public int Armor = 5;
public int Lives = 3;
public int PowerLevel = 1;
//-----------------------------------------------------------------------------------------------------------------------
void Update () {
//player Controlls
if (Input.GetKeyDown ("z")) {
StartCoroutine(MachGunzFire());
}
if (Input.GetKeyUp ("x")) {
Rigidbody clone;
clone = Instantiate(Missiles, transform.position, transform.rotation);
MissilesAmmount -= -1;
}
}
IEnumerator MachGunzFire (){
//every 0.5seconds instatiate Ridgid body.
yield return new WaitForSeconds(0.5);
Rigidbody clone;
clone = Instantiate(MachGunBullets, transform.position, transform.rotation);
}
}
Hi zak666,
Your problem is on all the lines where you instantiate your prefabs, because your clones are rigidbodys but your prefabs aren’t, thats exactly what your error says that you can’t convert a Object (your prefab) to a Rigidbody(your clone) there is an easy fix for that just add an “as Rigidbody” to all the ends like this:
clone = Instantiate(MachGunBullets, transform.position, transform.rotation) as Rigidbody;
as i said do this on all your lines where you instantiate a prefab.
Just an extra fact. If your clone would be a GameObject instead of a Rigidbody then you would have to add an as GameObject to the end of your instantiate line instead of as rigid body.
cheers skullbeats1