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);
}
}
// -------------------------------------------------------------------------------------------
}
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));
}
}
}
Your answer
Follow this Question
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