Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Wach3D · Apr 18, 2016 at 11:46 AM · c#loadingloadleveldisableenable

Pickup number of objects and have another object appear?

I made 6 pickup objects in my scene. At the end of the level there is a gameObject with Application.LoadLevel();. How do I make it so the Application.LoadLevel(); script is disabled BEFORE the player picks up all the pickup objects and enabled AFTER the player picks up all the pickup objects. Example: player picks up 5 pickup objects - script is disabled and player then picks up the 6th pickup object - script is immediately enabled.

Pickup script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PickUp : MonoBehaviour {
 
     public Text countText;
 
     private int count;
 
     // Use this for initialization
     void Start () 
     {
         count = 0;
         countText.text = count.ToString () + "/6" ;
         SetCountText ();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ("Pick Up")) 
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }
 
     void SetCountText ()
     {
         countText.text = count.ToString () + "/6" ;
     }
 }



Comment
Add comment · Show 3
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 Ali-hatem · Apr 18, 2016 at 12:12 PM 0
Share

why you do that if you just want to load level :

  void OnTriggerEnter(Collider other) 
  {
      if (other.gameObject.CompareTag ("Pick Up")) 
      {
          other.gameObject.SetActive (false);
          count = count + 1;
          SetCountText ();
            if(count == 6)
            // load level 
      }
  }

unless you have other reasons

avatar image Wach3D Ali-hatem · Apr 18, 2016 at 12:53 PM 0
Share

I don't want the player to have the ability to load the next level without picking up ALL the pickup objects FIRST. So I want the player to pickup 6 objects in total and then the player can go to the next level via Application.LoadLevel();. I don't want the player to pickup (for example) 5 objects and just move on to the next level, that's why I want to be able to enable and disable the script.

avatar image Ali-hatem Wach3D · Apr 18, 2016 at 01:04 PM 0
Share

but you have already added count = count + 1; which will increase the count variable by 1 each time picking one pickup object so logically if the count = 6 that means you have packed 6 objects correct me if i'm wrong

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Wach3D · Apr 20, 2016 at 06:15 AM

I figured it out! Thanks to all who answered, I really appreciate the help.

I simply added public int myObject;, added myObject.SetActive (false); in void Start () and added if (count ==6) { myObject.SetActive (true); } in void Update ()

The new script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PickUp : MonoBehaviour {
 
     public Text countText;
 
     public GameObject myObject;
 
     private int count;
 
     // Use this for initialization
     void Start () 
     {
         count = 0;
         countText.text = count.ToString () + "/6" ;
         SetCountText ();
         myObject.SetActive (false);
     }
     
     // Update is called once per frame
     void Update () {
         if (count == 6) 
         {
             myObject.SetActive (true);
         }
     
     }
 
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.CompareTag ("Pick Up")) 
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }
 
     void SetCountText ()
     {
         countText.text = count.ToString () + "/6" ;
     }
 }

I simplified the process (because I suck at scripting!) by making the gameObject with the Application.LoadLevel(); be disabled at the Start. So it basically isn't there at the Start. I duplicated the object at the same spot and removed the script component in it so it looks like it's there but the player can't use it UNTIL the player picks up all 6 pickups (hence the count ==6 in the if statement) and when the player does pick up all 6 pickups, the gameObject with the script Application.LoadLevel(); is enabled and the player can then use it.

If anything is unclear for any beginners out there (like me!), please feel free to comment. And again, thanks to all who answered, it's very much appreciated.

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 M-Hanssen · Apr 18, 2016 at 01:07 PM

In this case you can use a Static variable, but this is bad practice in general. Rather create a manager that keeps track of all pickups.

But anyway here you go:

     public Text CountText;
     public int CountBeforeLevelLoad = 6;
 
     // The use of a static variable is pretty dirty, best practice is to make an external manager or other entity to keep track of your picked up items!
     // But it does the trick in your case ;-)
     protected static int Count;
 
     protected void Start()
     {
         Count = 0;
         SetCountText();
     }
 
     protected void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pick Up"))
         {
             other.gameObject.SetActive(false);
             Count++;
             SetCountText();
             if (Count == CountBeforeLevelLoad)
             {
                 // Load your level
             }
         }
     }
 
     protected void SetCountText()
     {
         CountText.text = Count + "/6";
     }
Comment
Add comment · Show 1 · 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 Wach3D · Apr 19, 2016 at 03:58 PM 0
Share

Sadly, it didn't work. I was able to pick up less than 6 pickup objects and go straight to my gameObject with`Application.LoadLevel();` and go to the next scene without needing to pick up all 6 pickups.

Is there a way to disable the script on the gameObject property at Start and have it enabled when the player picks up all 6 pickups?

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.

Follow this Question

Answers Answers and Comments

143 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 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 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 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 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 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 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 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

enabling item after the other one is disabled and so on? 0 Answers

Cant Disable Script on Another Gameobject 1 Answer

How to activate an object after "x" seconds (C#)? 4 Answers

NullReferenceException: Object reference not set to an instance of an object ChildIdentifi.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/ChildIdentifi.cs:35) 0 Answers

Game Over Scene Efficient? 2 Answers


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