0
votes

I have 3 kinds of GameObject, namely blueBook, redBook and greenBook. There are 2 blueBook, 7 redBook and 4 greenBook in the scene.

They are each assigned with the following example script that have 3 properties.

public class blueBook : MonoBehaviour {

    public string type = "BlueBook";
    public string colour = "Blue";
    public float weight;
    float s;

    void Start () {
        float weightValue;
        weightValue = Random.value;
        weight = Mathf.RoundToInt (700*weightValue+300);
        s=weight/1000; //s is scale ratio 
        transform.localScale += new Vector3(s,s,s);
    }

    // Update is called once per frame
    void Update () {
    }
}

In the new class, I want to take all the variables of the GameObject (type, colour, weight) and store them inside an array. How to do this?

After they are all stored inside the array, user will input an weight. Then another class will search through all the info to delete both the array and GameObject(in the scene) with the same amount of weight.

Thank you.


UPDATE


blueBook.cs

public class blueBook: MonoBehaviour {

public string type = "blueBook";
public string colour = "Red";
public float weight;
float s;

void Start () {
    float weightValue;
    weightValue = Random.value;
    weight = Mathf.RoundToInt (500*weightValue+100);
    s=weight/1000; //s is scale ratio 
    transform.localScale += new Vector3(s,s,s);
    Debug.Log(weight);
}

// Update is called once per frame
void Update () {
}

}

block.cs

public class block: MonoBehaviour{

public List<GameObject> players;

    void Start()
    {   Debug.Log(players[1].GetComponent<blueBoook>().weight);
    }

    // Update is called once per frame
    void Update () {

    }

The debug.log for block.cs displayed 0 everytime eventho it display otherwise in debug.log of bluebook.cs. It is because it displayed the initial number? I don know wat is wrong

1
If the user inputs only weight, there would be no need for the other two properties to be stored? Or was that just an example and the user could insert either of the three? I would suggest building a dictionary with weight being the key and gameobject or rather a list of gameobjects the value: Dictionary<float, List<GameObject> >. You need something that gets all the objects (e.g. public arrays + inspector or finding via tag), that sorts those into the dictionary (if there is a key of that weight, append the list, otherwise create a new entry). - Gunnar B.
The variables are in the script. so each already have their own variable. user input will only delete the item and nothing else. Can you show me how to do it with the coding example? - whoami
can i store those gameobjects in array and access the variables back? - whoami
Yes, you can simply do something like myArray[0].GetComponent<blueBlock>().weight. This would access the first element in the array, get it's blueBlock script component and then access the weight member. Do you want to store all blocks in the same array? Your question is not quite clear about that. - Gunnar B.
since i cnt find a way to do that. In a new class, i group all types of gameobjects into array using tags "players = GameObject.FindGameObjectsWithTag ("Player"); ". but then again, i donno how to access weight and other variables of the gameobjects - whoami

1 Answers

3
votes

For all blocks in one list you can create a script that has a public list and you drag all your gameobjects into the list in the inspector.

using System.Collections.Generic;

public class ObjectHolder : MonoBehaviour
{
    public List<GameObject> theBooks;

    // You can remove by weight e.g. like this
    public void DeleteByWeight(float inputWeight)
    {
        for(int i = theBooks.Count - 1; i >= 0; i--)
        {
            if(theBooks[i].GetComponent<Book>().weight == inputWeight)
                Destroy(theBooks[i]);
                theBooks.RemoveAt(i);
            }
        }
    }
}

The script on the blocks needs to be renamed to the same name for all (Book in my example). From your code that is no problem since they only differ in the value of the members.