• 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
Question by karl_ · Jan 13, 2011 at 06:04 AM · arrayplayerprefsintplayerprefsx

Why is this giving me an error? (ToBuiltin problems)

I'm not quite sure what's happening here. I'm getting the error "InvalidCastException: Cannot cast from source type to destination type." However, I can't see anything wrong. Here is the code:

var locX : float = 5; var locY : float = 5; var sizeX : int = 60; var sizeY : int = 40; var buttonText : String; var pieceToCreate : GameObject; var positionToSpawn : Vector3 = Vector3(-3,0,0);

// Save variables var builtInSaveArrayX : int[]; var builtInSaveArrayY : int[]; var builtInSaveArrayZ : int[]; var isSaveButton : boolean = false; private var saveItem : Transform;

function OnGUI () { // Declare Skin

 if (GUI.Button (Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)), sizeX, sizeY), buttonText)) 
 {
     if(!isSaveButton)
         Instantiate(pieceToCreate, positionToSpawn, transform.rotation);
     else
         saveIt();
 }

}

function saveIt() { var userObject : GameObject[] = GameObject.FindGameObjectsWithTag("moveable"); var closest : GameObject; var closestDist = Mathf.Infinity;

 var saveArrayX : Array = new Array();
 var saveArrayY : Array = new Array();
 var saveArrayZ : Array = new Array();

 var itemsOnScreen : int;

 for (saveItem in userObject) 
 { 
     itemsOnScreen++;
     //   Augment array and store pos. data  
     saveArrayX.length = itemsOnScreen-1;
     saveArrayY.length = itemsOnScreen-1;
     saveArrayZ.length = itemsOnScreen-1;

     saveArrayX[itemsOnScreen-1] = saveItem.transform.position.x;
     saveArrayY[itemsOnScreen-1] = saveItem.transform.position.y;
     saveArrayZ[itemsOnScreen-1] = saveItem.transform.position.z;
 }

 builtInSaveArrayX = saveArrayX.ToBuiltin(int);

// builtInSaveArrayY = saveArrayY.ToBuiltin(int); // builtInSaveArrayZ = saveArrayZ.ToBuiltin(int);

 if (!PlayerPrefsX.SetIntArray("savedLevel1_xValues", builtInSaveArrayX))
 {
     Debug.Log("oops, the gameobject array saving failed terribly");
 }
 if (!PlayerPrefsX.SetIntArray("savedLevel1_yValues", builtInSaveArrayY))
 {
     Debug.Log("oops, the gameobject array saving failed terribly");
 }
 if (!PlayerPrefsX.SetIntArray("savedLevel1_zValues", builtInSaveArrayZ))
 {
     Debug.Log("oops, the gameobject array saving failed terribly");
 }

 Debug.Log("Total items " + itemsOnScreen + "   Size of array " + saveArrayX.length);    
 Debug.Log("Positions " + saveArrayX);

}

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Jessy · Jan 13, 2011 at 01:46 PM

saveItem.transform.position.whatever is a float. (Vector3s are 3 floats.)

If your values are going to be floating-point representations of integers, you can convert each one to an int using parseInt(//float goes here), or one of these Mathf functions. Also, there's no reason to use Arrays anymore. Use Lists, and then you have ConvertAll available to you.

Comment
karl_

People who like this

1 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 karl_ · Jan 13, 2011 at 02:25 PM 0
Share

Did not know about lists. Thanks.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Attempting to store an Inventory Array in PlayerPrefsX 1 Answer

How to save an array to PlayerPrefs? 1 Answer

assigning arrays with for in 1 Answer

For In Loop Fills all values in arrays problem 2 Answers

Saving Array Objects in Android. 0 Answers


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