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 /
avatar image
1
Question by sa201997 · Jul 12, 2018 at 07:13 AM · collisiontriggerstartcoroutinecoin

I want to active my coins again after few minutes ?

My player collides with the coin, and it the coin is deactivated in the hierarchy? But after a few seconds, I want to my coin should be activated again? I have done the code, but to coins are not reactivating? Please help me with this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Coin_Score : MonoBehaviour {
 
   
     public Text score;
     public GameObject coin;
     private int scoreupdate;
 
     public  bool cointrue = true;
     
 
 
     // Use this for initialization
     void Start () {
 
         score.text = "0" ;
         scoreupdate = 0 ;
        
     }
 
    
 
    
     private void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.CompareTag("Coins"))
         {
 
            other.gameObject.SetActive(false);
             scoreupdate = scoreupdate + 5   ;
             cointrue = false;
 
 
             SetCountText();
 
        }
 
     }
     void SetCountText()
     {
         score.text = scoreupdate.ToString();
     }
     void Lateupdate()
     {
         StartCoroutine(activecoin());
     }
 
     IEnumerator activecoin()
     {
         if (cointrue == false)
         {
             if (GameObject.FindGameObjectWithTag("Coins").activeInHierarchy == false)
             {
                 yield return new WaitForSeconds(5.0f);
                 GameObject.FindGameObjectWithTag("Coins").SetActive(true);
                 cointrue = true;
             }
         }
     }
 
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by swanne · Jul 12, 2018 at 07:36 AM

I would suggest rather than activating and deactivating your coins, you should spawn and destroy your coins. A coin will be a prefab and have a script with OnCollisionEnter() Destroy this.gameObject - if the other collider tag is "Player". Then on the playerController script, you could have a coroutine which spawns the coin prefab after 5 seconds at the position of where is what collected by the player, again in an OnCollisionEnter() function on the playerController 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 mayur7garg · Jul 12, 2018 at 09:11 AM

@sa201997 Functions like Start(), Update(), LateUpdate(), etc. are MonoBehavior components and are called only for objects active in hierarchy. So as soon as the coin goes inactive, LateUpdate() of this script stops being called. Consequently, the coroutine is never started. This can be resolved by calling this coroutine from another script which is attached to a gameObject which remains active. And by the way, it is LateUpdate(), not Lateupdate(). Happy to help!

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 AdityaViaxor · Jul 12, 2018 at 11:57 AM

Have you placed few coins in scene manually and when you collide them they set inactive if this is true then the problem with your code is inside activate coin you check for object in hierarchy with tag "coins" is active or inactive and suppose there are 4 to 5 coin with tag coins the you if condition (if(GameObject.FindGameObjectWithTag("Coins").activeInHierarchy == false)) get false and compiler goes out of condition.

try using object pooling for stuff like this, just google for object pooling in unity and you will find soloution. make a pool of coins in one script and invoke them in another script like game manager and set inactive when collide with player.

calling activeCoin from update is very bad practice in future if you game grow these things will cause lagging. try not to use update or use as less as possible.

thank you

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

152 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to handle animation of multiple Coins? 1 Answer

OnTriggerEnter2D/OnCollisionEnter2D - delay 1 Answer

Collision detection for low relative velocity, high world velocity rigidbodies 2 Answers

How to call OnTriggerEnter once 5 Answers

Collide crafting system 0 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