Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bogs101 · Jan 09, 2018 at 04:40 AM · arraylistreferencevaluetypes

changing reference types to value types

Hello, I have an array of all the gameobject in the scene as well as an integer associated to each gameobject. I would like to randomly select a number of these objects and place them in a list using their associated value. I then want to be able to reset the value to zero when all selectects have been added to the list. The problem is that I have it setup where the list integer is a reference to the array integer. This means that when I modify the array values I also end up changing the associated list item it is referencing to.

I don’t know how to add items to the list as value types instead of reference types. Or change reference types to value types.

Could someone please help me out with this.

ty.

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class GameObjectManager : MonoBehaviour {

 [System.Serializable]
 public class GOD
 {
 public GameObject myGameObject;
 public int myValue;

     public GOD (GameObject myGameObject, int myValue)
     {
         this.myGameObject = myGameObject;
         this.myValue = myValue;
     }

 }
 public GOD[] gameobjectArray;

 public List<GOD> GameObjectList = new List<GOD>();

 public int numberOfSelections = 5;
 public int numberOfObjects = 20;


 // Use this for initialization
 void Start () {

     numberOfSelections += 1; // this is just to adjust the number of selections to discount the zero later on. 
     GetDestinations ();            // randomly selects objects from the orbject array.

 }
 // ----------------------------------------------------------------------------------------

 void GetDestinations () {
     // randomly select 5 items from main list and mark there value field with an incrementing number.
     for (int i = 1; i < 6; i++) {
         //get a random item in the main array and give it a value of i
             int RandomArraySelection = 0;
         do {
             RandomArraySelection = Random.Range (0, 20);    // get a random number.
         } while (RandomArraySelection != i);                // only get a random number if it's not already = to i.
         gameobjectArray [Random.Range (0, 20)].myValue = i;
     }

     // Add the array items into the list which do not have a value of 0.
     for (int value = 1; value < numberOfSelections; value++) {
         for (int i = 0; i < numberOfObjects; i++) {
             Debug.Log ("hello");
             if (gameobjectArray [i].myValue == value) {
                 GameObjectList.Add (gameobjectArray [i]);
             }
         }
     }

     // reset array items to zero
     for (int i = 0; i < 20; i++) {
         Debug.Log (gameobjectArray [i].myGameObject);
         gameobjectArray [i].myValue = 0;
     }

     for (int i = 0; i < GameObjectList.Count; i++) {
         Debug.Log (GameObjectList[i].myValue + " " + GameObjectList[i].myGameObject);
     }
 }
 // -------------------------------------------------------------------------------------------

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bogs101 · Jan 10, 2018 at 12:58 AM

I was able to solve this problem for myself, so thought i would share it.

By creating a couple of local variables for each class variable. i could then transfer the contents of the array item into these variables. then i just needed to add them to GameObjectList by putting them in a new class Reference type. here's the amended code.

     // Add the array items which do not have a value of 0 into the list.
     for (int value = 1; value < numberOfSelections; value++) {
         for (int i = 0; i < GameManager.numberOfObjects; i++) {
             if (GameObjectData.gameobjectArray [i].myValue == value) {
                 int thisvalue = GameObjectData.gameobjectArray [i].myValue;
                 GameObject thisGameObject = GameObjectData.gameobjectArray [i].myGameObject;
                 GameObjectList.Add (new GameObjectData (thisGameObject,thisvalue));
             }
         }
     }
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

132 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

[common programming question] Custom array returns nothing 2 Answers

Some LINQ functions won't work. And no; not iOS 0 Answers

Storing positions and booleans together 1 Answer

only keep every 10th entry in an array? 2 Answers

Question about array 2 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