0

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

6
  • 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). Commented Apr 12, 2016 at 7:13
  • 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? Commented Apr 12, 2016 at 7:24
  • can i store those gameobjects in array and access the variables back? Commented Apr 12, 2016 at 7:58
  • 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. Commented Apr 12, 2016 at 8:21
  • 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 Commented Apr 12, 2016 at 8:30

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

17 Comments

can the scripts be renamed to same name?
You can rename a class, yes. You just need to make sure you rename the file too. You would need only one of those scripts then. Set the appropriate names/types in the inspector. (You could also just create a new script and copy over.)
it wont display when i use Debug.Log(theBlocks[i].GetComponent<Block>().weight == inputWeight);
error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Basket.Start () (at Assets/scripts/Basket.cs:13)
Which line is that? The destroy? This should work, I can't test it right now though. Your error means that you try to access an element of the list that doesn't exist.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.