• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eeveelution8 · Aug 10, 2013 at 12:23 AM · errorerror-message

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · Aug 10, 2013 at 01:07 AM 0
Share

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 assuming 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)];
avatar image eeveelution8 clunk47 · Aug 10, 2013 at 01:20 AM 0
Share

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?

avatar image clunk47 clunk47 · Aug 10, 2013 at 03:01 PM 0
Share

You need to fill your array.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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];

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eeveelution8 · Aug 10, 2013 at 01:31 AM 0
Share

I'm still unclear what to do, I tried replacing the part you wrote, but it has the same error, what do i do?

avatar image hoy_smallfry · Aug 10, 2013 at 01:33 AM 0
Share

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?

avatar image hoy_smallfry · Aug 10, 2013 at 01:35 AM 0
Share

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.
avatar image eeveelution8 · Aug 10, 2013 at 01:36 AM 0
Share

what my goal is, is to have it make an item, then stop

avatar image eeveelution8 · Aug 10, 2013 at 01:42 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,why cant i build my project 0 Answers

[Solved] AutoType script doesn´t work! 1 Answer

need help, with greater than statment 1 Answer

syntax errors 1 Answer

I need help with the eval() function of javascrit 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges