Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 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)];
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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges