• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Asunez · Oct 09, 2013 at 06:24 PM · pickupdisappearobjective

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 :(

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 09, 2013 at 06:30 PM 0
Share

You are likely to get more help if you edit your question and include the script (with your attempted changes). Few will go to the trouble of downloading the project and combing through the source files to help you.

avatar image rodrigodelucaetuma · Jul 23, 2015 at 05:51 PM 0
Share

I'm also new and I'm trying the same thing! And I have the same problem (with only a slight difference, I don't want the wall to dissapear, I want it to stop being kinematic)... it still doesn't work! Does the wall game object need a tag?

using UnityEngine; using System.Collections;

public class playercontrol : $$anonymous$$onoBehaviour { public float speed; public GUIText countText; public GUIText winText; private int count;

 void Start () {
     count = 0;
     SetCountText ();
     winText.text = "Chabiliii! $$anonymous$$ova 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();
         
         if(count>=12)
         {
             levelWall.gameObject.is$$anonymous$$inematic(false);
         }
     }
 }

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

}

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by KBurchfiel · Jun 01, 2016 at 02:40 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by rodrigodelucaetuma · Jul 24, 2015 at 07:40 AM

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!!!";
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by luna_C · Jan 14, 2014 at 10:41 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Understanding the Pickup script in Lerpz tutorial 2 Answers

How to collect items to add up 1 Answer

Weapon pick up 3 Answers

Help with pickup script 0 Answers

How would I go about removing a mesh renderer component on collision with a trigger? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges