How to make a certain object disappear after a set amount of pickups is collected?

First of all, I would like to note that I really am a newbie at this, and I guess this question is trivial, but still, I need help with this.

So basically, I’ve been following [this][1] tutorial and finished it successfully. I wanted to add some more levels to it, and I created another planes adjacent to the first one. Now what I want to do is separate these planes with walls (success) and then, after all pickups are collected in “map” then make first wall disappear and after other collectibles are collected the second wall, and so on and so on.

I’m not that new to C#, but still get it quite confusing. I tried editing the PlayerController script (where the collectibles are counted and the message after collecting them all is generated) to do this, but failed hard.

I know that the way to do so is to use other.gameObject.SetActive(false); and that it probably should be in void Update() (or void FixedUpdate?) and an if clause, but I can’t get this to work.

I tried searching here and with Google, but to no help. Maybe due to bad search query, but I don’t really know how to ask for this in few words. I would really appreciate your help, cause making things in Unity is great fun, but I’m really stuck here :frowning:

EDIT:-----------------------

So, this is my PlayerController script that also counts pickups… well, picked up:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
	public float speed;
	public GUIText countText;
	public GUIText winText;
	public int count;
	
	void Start()
	{
		count = 0;
		SetCountText();
		winText.text = "";
	}
	
	void FixedUpdate ()
	{
		float moveHorizontal = Input.GetAxis("Horizontal");
		float moveVertical = Input.GetAxis("Vertical");
		
		Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
		
		rigidbody.AddForce(movement * speed * Time.deltaTime);
		
		if(count>=11)
		{
			//What goes here?
		}
	}
	
	
	void OnTriggerEnter(Collider other)
	{
		 if(other.gameObject.tag == "Pickup")
		{
			other.gameObject.SetActive(false);
			count = count + 1;
			SetCountText();
		}
		
		 if(other.gameObject.tag == "Detektor")
		{
			Application.LoadLevel(Application.loadedLevel);
		}
	}
	
	void SetCountText()
	{
		countText.text = "Zebrano: " + count.ToString();
		if(count >= 11)
		{
			winText.text = " WYGRYWASZ! ";
		}
	}
}

My guess is that I need to add something in the place I marked with a comment, 'cause it needs to be checked all the time and after it triggers, then it should destroy a cube named “Dzialowa”. And also i suppose that it has to be something with mentioned earlier gameObject.SetActive(false)

BTW. Sorry for some polish words in the code, I know it may be confusing a bit ;p
[1]: http://unity3d.com/learn/tutorials/projects/roll-a-ball

I am going to assume you want to de-activate ( SetActive(false) ) the wall that is separating the different “maps” when the player has collected a certain numberof blocks; because I am not too sure I understand what you want correctly.

void FixedUpdate ()
    {
       float moveHorizontal = Input.GetAxis("Horizontal");
       float moveVertical = Input.GetAxis("Vertical");
 
       Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
       rigidbody.AddForce(movement * speed * Time.deltaTime);
 
       if(count>=11)
       {
         wallName.gameObject.SetActive (false);
       }
    }

Replace “wallName” with the name of the wall you are trying to de-activate. In this case, I think your “wallName” is “Detektor”, but again I’m not too sure.

You could alternatively de-activate the wall after the trigger event, when the sufficient number of block were collected:

void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == "Pickup")
       {
         other.gameObject.SetActive(false);
         count = count + 1;
         SetCountText();

         if(count==11)
        {
             wallName.gameObject.SetActive(false);
        }
       }
    }

Again replace “wallName” with the name of your wall.

I solved it with a destroy command line!

using UnityEngine;
using System.Collections;

public class playercontrol : MonoBehaviour
{
public float speed;
public GUIText countText;
public GUIText winText;
private int count;

void Start () {
	count = 0;
	SetCountText ();
	winText.text = "Chabiliii! Mova a bola usando as flechas do teclado e recolha os cubos!";
}

void Update () {
	float moveHorizontal = Input.GetAxis ("Horizontal");
	float moveVertical = Input.GetAxis ("Vertical");
	
	Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
	
	GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
	
}

void OnTriggerEnter(Collider other)
{
	if (other.gameObject.tag == "pickup") {
		other.gameObject.SetActive (false);
		count = count + 1;
		SetCountText ();
	}
}

void OnCollisionEnter (Collision col)
{
	if(col.gameObject.name == "levelwall" && count >= 12)
	{
		Destroy(col.gameObject);
	}
}

void SetCountText (){
	countText.text = "Pontos: " + count.ToString ();
	if (count >= 5) {
		winText.text = "";
	}
	if (count >= 12) {
		winText.text = "Parabens Chabiliiii!!! Agora derruba a parede!!!";
	}
}

}

Here’s how I’m doing it in my game. I have two barriers that I define as game objects (wall1 and wall2). I count the ‘grape’ pickups that I’ve gotten so far, and I then tell the game to destroy wall1 and wall2 once the count reaches 3 and 6, respectively. You just have to tell the game what your “wall1” and “wall2” are in the inspector.

I also include a user interface element for counting the pickups and giving a ‘win’ message. Most of this is copied and pasted from Unity’s RollABall tutorial.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ExtraScript : MonoBehaviour {

public GameObject wall1;
public GameObject wall2;
public Text countText;
public Text winText;
private int count;

void Start()

{
    count = 0;
    SetCountText ();
    winText.text = "";

}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag ("Grape"))
    {
        other.gameObject.SetActive(false);
        count = count + 1;
        SetCountText();
    }
}

void Update()
{
    if (count == 3)
        {
        Destroy(wall1);
        }
    if (count == 6)
    {
        Destroy(wall2);
    }
}

void SetCountText ()
{
    countText.text = "Count: " + count.ToString();
    if (count >= 7)
    {
        winText.text = "You have won!";
    }

}

}

End script