Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Omega2077 · Jul 03, 2015 at 01:56 PM · playerscore

Does any know how to hide and show 2d sprites in unity 5 by using a 2d boxcollider set to trigger when player collides with the boxcollider

I would really like to know if anybody knows how to hide and show gameobjects in unity 5, either by disabling and re-enabling it after 4secs, or even by someother method that I have not heard of yet. At the moment my gameobject being a coin works great when the player collides it collects the coin and adds a point to my score system, but I have written the code to destroy the gameobject once triggered because I do not know how to hide it temporarily, so if anyone knows how to do this please do share because I really need to know asap my code is visible below for you all to analyse the problem is on line 15 the Destroy (gameObject); is down the bottom.

 using UnityEngine;
 using System.Collections;
 
 public class CoinPickUp : MonoBehaviour {
 
     public int pointsToAdd;
 
     void OnTriggerEnter2D (Collider2D other)
     {
         if (other.GetComponent<LopterMovement> () == null)
             return;
 
         ScoreManager.AddPoints (pointsToAdd);
 
         Destroy (gameObject);
     }
 }
 





Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jprocha101 · Jul 03, 2015 at 02:08 PM

You can use the statement below to instead of destroying it. That will simply "hide" it from being active in the scene.

 gameObject.SetActive(false);

You might also want to think about pooling for objects such as coins and other prefabs that are instantiated multiple times. You can search the asset store for "pooling" to save you the time of coding it yourself.

Comment
Add comment · Show 6 · 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 Cherno · Jul 03, 2015 at 02:10 PM 1
Share

^ .enabled in connection with gameObejct is deprecated, it should only be used for components nowadays. Ins$$anonymous$$d, gameObject.SetActive(false) is to be used.

Also, you can hide a sprite by disabling it's SpriteRenderer component.

 GetComponent<SpriteRenderer>().enabled = false;
avatar image jprocha101 · Jul 03, 2015 at 02:12 PM 0
Share

Yes, thank you, you are correct. I have updated my answer.

avatar image Omega2077 · Jul 04, 2015 at 12:30 PM 0
Share

The code worked so thank you all, except it won't re-render the sprite again once the coin has been collected and it needs to re-render after 4 seconds.

avatar image jprocha101 · Jul 05, 2015 at 02:41 AM 0
Share

How are you re-rendering the coin?

avatar image Omega2077 · Jul 05, 2015 at 05:27 AM 0
Share

that's the problem I do not know how to re-render it

Show more comments
avatar image
0

Answer by etopsirhc · Jul 05, 2015 at 08:46 AM

considering you want to reactivate it later, i would set the rigidbody and the renderer to be disabled, then invoke a function to reactivate them.

code below was not spellchecked or checked for proper setup. but the idea is there so it shouldnt be too hard to find the functions/properties/classes needed to make it work.

  using UnityEngine;
  using System.Collections;
  
  public class CoinPickUp : MonoBehaviour {
  
      public int pointsToAdd;
      SpriteRenderer rend;
      Rigidbody2D rb2;
 
      void start(){
          rend = gameObject.GetComponent<SpriteRenderer>();
          rb2 = gameObject.GetComponent<RigidBody2D>();
      }
  
      void OnTriggerEnter2D (Collider2D other)
      {
          if (other.GetComponent<LopterMovement> () == null)
              return;
  
          ScoreManager.AddPoints (pointsToAdd);
          
          rb2.enabled = false;
          rend.enabled = false;
          invoke(Enabler,4f);
          
      }
  
      void Enabler(){
          rb2.enabled = true;
          rend.enabled = true;
      }
  }
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Organising player Score from highest to lowest? 2 Answers

Getting playerID from the Google Play Leaderboard? 0 Answers

Score Question Java Script, problem 1 Answer

high score script? 0 Answers

live system for sword 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