Bean
Bean

Reputation: 33

Resizing a List of GameObjects in Unity

I've been working on a game that allows a user to create targets by clicking on a designated area. I'm trying to implement it so that, while the game will store information for all targets the user creates, only a maximum of 9 targets will ever be shown at once (a series of 'pages' if you will).

The problem is I'm having trouble 'clearing' the area. I'm trying to clear targets by changing their scale to 0/0/0

if(newPage)
            {
                print("Making New Page...");
                //"Clear" the screen of Targets
                foreach(GameObject obj in targets)
                {
                    obj.gameObject.transform.localScale = new Vector3(0.0f , 0.0f, 0.0f);
                    //print(obj);
                }

                newPage = false;
                totalPages++;
                currentPage++;
            }

Some helpful things:

targets is the name of the List
newPage is a boolean used to indicate when to run this code
totalPages and currentPages are for reactivating the targets later

The two print statements are both functional, but none of the targets actually disappear. I'm not really sure why it's not working.

Upvotes: 1

Views: 136

Answers (1)

capnbishop
capnbishop

Reputation: 83

Rather than shrinking the object, it may be easier to deactivate it with the GameOjbect.SetActive() function:

if(newPage)
    {
        print("Making New Page...");
        //"Clear" the screen of Targets
        foreach(GameObject obj in targets)
        {
            obj.SetActive(false);
            //print(obj);
        }

        newPage = false;
        totalPages++;
        currentPage++;
    }

If you want the object to be active in the scene, but simply invisible, then another option is to disable the MeshRenderer component:

obj.GetComponent<MeshRenderer>().enabled = false;

Upvotes: 1

Related Questions