- Home /
How to make an object appear when another is collected?
Hi, i'm new to unity, I have made roll-a-ball game following the tutorials on unity. now i want change it so only 2 pick up objects are displayed when you open the game and then when you collect them another 2 of the pick up objects appear. Please help me!
Answer by juicyz · Aug 30, 2016 at 05:16 PM
I haven't looked at the tutorial yet but I'm guessing the amount of objects are predefined, as in they are all created at initialization of the game by the Scene and not Script.
There are many ways to do this.
1- Spawn in the objects into the Scene and have a list of all the pickup objects.
This involves setting them all as 'SetActive(false)' then randomly selecting two at the beginning and doing a 'SetActive(true)'
2- Use the GameObject.Find methods on 'Picks Ups' and select which children you want to set asActive
3- etc
Next have a counter of some sort that increments when a player hits a pick up object, in your Update function, you can check if the counter is equal to 2, then set two random pick up objects as 'SetActive(true)'
If you need actual code code, I can write that too but figuring it out on your own is much more fun.
Hi thanks so much for the reply! i have created the SetActive(false) and put the scrip attached it the the parent and its working! but i don't know how to select 2 objects to make SetActive(true)?
How are your objects organized?
Do you have an array or list of them somewhere in code? If you do, then you can select from this list randomly using something like: int random = (int)(Random.value() * (array length)) Do this, set the object to active, do this again and set object to active
Are they just placed in the game by drag and drop? You could gather the objects by using a Find method and setting it to active, then getting another and setting it to active
Answer by Cynikal · Aug 30, 2016 at 05:53 PM
The simplest solution to this that I can think of, is as follows:
Make a Game Manager empty gameobject on your scene. Make a GameManager script for said game object.
In your GameManager, have an array of predetermined objects to pick up.
In your pickup objects, when you collide, call a function on GameManager that they've been picked up. Disable the pickup object.
On GameManager, when called "PickedUp" function, add your points or whatever, then randomly select 2 gameobjects from the array and enable them.