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 Sam-Robichaud · Jul 24, 2012 at 08:24 AM · arrayrandomcast

Unique # Generation "Cannot cast from source type to destination type."

I'm trying to generate 3 unique random numbers out of 6, i'm getting a "Cannot cast from source type to destination type."

I'm kinda new to this and muddling my way trough. I know it has something to do with trying to pass a variable type in a container not set for it, but I cant for the life of me figure out how to correct this?

any help appreciated.

 #pragma strict
 
 var randomNumbers: ArrayList;
 
 function Start ()
 {
 
  var aNumbers = new Array(7);
     for (var i = 0; i < 7; i++) 
     {
        aNumbers.Add(i);
     }
 
  var randomNumbers = new Array[3];
  
  for (i = 0; i < randomNumbers.Length; i++) 
     {
     var thisNumber = Random.Range(0, aNumbers.Count);
     randomNumbers[i] = aNumbers[thisNumber];   **//Cast Error Referenced here**
      aNumbers.RemoveAt(thisNumber);
     }
 }
 
 function Update ()
 {
 print (randomNumbers[0]);  **//Cast Error Referenced here**
 print (randomNumbers[1]);
 print (randomNumbers[2]);
 }
Comment
Add comment
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

3 Replies

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

Answer by Bunny83 · Jul 24, 2012 at 02:07 PM

This line:

 var randomNumbers = new Array[3];

will create a native array of the Array object type. So the elements in the native array would be Array objects. That doesn't make much sense in your case. You want to store integer values (int).

You should avoid the Array class in general since it's slow and not type-safe. Use a generic List instead:

 #pragma strict
 import System.Collections.Generic;
 
 var randomNumbers : int[];
 
 function Start ()
 {
     var aNumbers = new List.<int>();
     for (var i = 0; i < 7; i++) 
     {
         aNumbers.Add(i);
     }
     
     randomNumbers = new int[3]; // create the native array with 3 elements
 
     for (i = 0; i < randomNumbers.Length; i++) 
     {
         var thisNumber = Random.Range(0, aNumbers.Count);
         randomNumbers[i] = aNumbers[thisNumber];
         aNumbers.RemoveAt(thisNumber);
     }
 }
 
 function Update ()
 {
     print (randomNumbers[0]);
     print (randomNumbers[1]);
     print (randomNumbers[2]);
 }
Comment
Add comment · Show 1 · 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 Sam-Robichaud · Jul 24, 2012 at 05:21 PM 0
Share

Thank you, That fixed it.

I really appreciate the help.

avatar image
1

Answer by BeHappy · Jul 24, 2012 at 11:25 AM

Array[] and Array() both are different...so you are getting the error "Cannot cast from source type to destination type."

So try to change them accordingly and use Array[] and assign values directly in the inspector window of Unity Editor.

Comment
Add comment · Show 4 · 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 Sam-Robichaud · Jul 24, 2012 at 11:40 AM 0
Share

That seems to have corrected the casting error, however now it gives me a new error.

when I try to print the results.

print (randomNumbers[0]);

//NullReferenceException: Object reference not set to an instance of an object

any thoughts?

avatar image BeHappy · Jul 24, 2012 at 11:45 AM 0
Share

Ins$$anonymous$$d of print try Debug.log().

Debug.Log(randomNumbers[0]);

avatar image Sam-Robichaud · Jul 24, 2012 at 11:48 AM 0
Share

that didn't change anything :(

avatar image Kryptos · Jul 24, 2012 at 01:02 PM 1
Share

print is a shortcut for Debug.Log, no wonder (see: http://answers.unity3d.com/questions/10108/debuglog-or-print-whats-the-difference-and-when-to.html)

avatar image
0

Answer by C$E Luffy · Jul 24, 2012 at 12:57 PM

you are using a global "randomNumbers" and a local inside the Start(). you are only initializing the local one not the global one. In update function global "randomNumbers" is used which is not iniatialized. remove the "var" keyword from " var randomNumbers = new Array[3];" in Start(). use only "randomNumbers = new Array[3];"in Start().

Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Store multiple random integers in an array? 4 Answers

Randomly Choosing two objects from an array and switching their positions 3 Answers

C# Array with random.range help? 2 Answers

how to check for GameObject is null in array with random 1 Answer

How can I Instantiate an Object from a Randomly Selected Index in an Array of Game Objects? 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