- Home /
how do i fix this error
i get this error,
Assets/custom/CUSTOM/Scripts/randomspawnn.js(7,48): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(int, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
from this script,
var items : GameObject [];
var thePrefab = Random.Range(1,items.length);
function OnTriggerEnter() {
collider.enabled = false;
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
//------------//
}
how do i fix this?
thePrefab is defined as an int. It needs to be a GameObject. I see you're trying to assign it as an index, a random one, I'm assu$$anonymous$$g from items. To call an indexed GameObject from items, define thePrefab as a GameObject, then get the index like so:
var thePrefab : GameObject = items[Random.Range(1,items.length)];
it now shows the error:
NullReferenceException: Object reference not set to an instance of an object UnityScript.Lang.Extensions.get_length (System.Array a) randomspawnn..ctor () (at Assets/custom/CUSTO$$anonymous$$/Scripts/randomspawnn.js:2)
whats wrong still?
Answer by hoy_smallfry · Aug 10, 2013 at 01:19 AM
This is referring to the parameters that you are putting into Instantiate
. It's saying that there are no versions of this function that take an int
, a Vector3
, and a Quaternion
, in that order.
If you look at the documentation for Instantiate, you'll that the function has only one version: one that takes a an Object
, a Vector3
, and a Quaternion
. If you look at your code, the variable thePrefab
is in fact an int
type. This explains why you are getting this error. it is asking for an Object
, and you are giving it an int
instead.
Your real problem is that you are misusing Random.Range
. According to the documentation, it returns a random number between the two numbers you gave it.
Assuming what you meant to do was use Random.Range
to get the index, you should use the result of Random.Range
to get the item you want from items
, using the []
operator:
var items : GameObject [];
var index : int = Random.Range(1,items.length);
var thePrefab : GameObject = items[index];
I'm still unclear what to do, I tried replacing the part you wrote, but it has the same error, what do i do?
What's the behavior that you want?
Do you want a different copy every time the the trigger happens? or do you want the it pick a random object once and then create copies of that same thing over and over every time a trigger happens?
If you want it to pick a new random every trigger, then you need to have it generate a new index every time OnTriggerEnter
is called, like this:
var items : GameObject [];
function OnTriggerEnter()
{
collider.enabled = false;
// picks a new number in that range.
var index : int = Random.Range(1,items.length);
// gets the prefab there at that index
var thePrefab : GameObject = items[index];
//... now use it.
what my goal is, is to have it make an item, then stop
i just got it to work, i used that code, and it wouldn't work, but i realized the trigger boolean on the collider was off. so i turned it on, and it worked perfectly :)
thanks for the help
Your answer
Follow this Question
Related Questions
Debug.Log error 2 Answers
Tutorial script problem 1 Answer
GetComponent Picking up multiple scripts from differrent objects? 1 Answer
Input Key Named: Enter is unknown error, Door open on keydown 1 Answer
What is wrong with my script? 1 Answer