I know my title seems weird but it’s a real problem that I am having. I get an error that says "error CS0029: Cannot implicitly convert type Int UnityEngine.GameObject’ ". Considering my script it makes sense that i would get this error, but I don’t how to fix it. Here is my script
using UnityEngine;
using System.Collections;
public class MagicBox : MonoBehaviour {
public GameObject[] Guns;
// Use this for initialization
void OnTriggerStay () {
if (Input.GetKey ("x")) {
int GunSpawn = Random.Range (0, Guns.Length);
GunSpawn //Moretexthere;
}
}
}
You are trying to assign the return value of Random.Range(int min, int max) which is an int type to the Gunspawn variable which is a GameObject type, that’s why the error is popping, what you’re actually looking for are the GameObjects stored into the Guns array so you need to change your code to.
GameObject GunSpawn = Guns [Random.Range (0, Guns.Length)];
By the way, when you need to post additional info or make corrections to your question you should Edit by clicking on the gear icon on the top right corner of your question instead of posting the edits as an answer which will cause clutter to the thread.
@Glurth Oh wait, I changed it on accident sorry Here is the errored code.
using UnityEngine;
using System.Collections;
public class MagicBox : MonoBehaviour {
public GameObject[] Guns;
// Use this for initialization
void OnTriggerStay () {
if (Input.GetKey ("x")) {
//error below, IDK what to do at the moment
GameObject GunSpawn = Random.Range (0, Guns.Length);
GunSpawn.SetActive (true);
}
}
}
`
Hey mate, you’re looking to use that random numer as an idea to find a game object in your Guns collection.
int gunIndex =Random.Range (0, Guns.Length);
GameObject GunSpawn = Guns[gunIndex];