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 YoloJoe · Feb 05, 2016 at 06:39 PM · rendereroncollisionenterblinking

Trouble combining two simple scripts

Hello :)

When I hit a object tagged "RemovableObject", I want the object thats been hit to be deactivated and be activated again after 10 seconds. That part works with this script:

 using UnityEngine;
 using System.Collections;
 
 public class RemovableObject : MonoBehaviour
 
 {
     IEnumerator OnCollisionEnter (Collision col)
     {
         if(col.gameObject.tag == "RemovableObject")
         {
             col.gameObject.SetActive (false);
             yield return new WaitForSeconds (10);
             {
                 col.gameObject.SetActive (true);
             }
 
         }
 }

Now I want to make the object blink before appearing again, so I found this script:

 void Start () 
     {
         StartCoroutine(DoBlinks(3f, 0.2f));
     }
 
     IEnumerator DoBlinks(float duration, float blinkTime) 
     {
         while (duration > 0f) 
         {
             duration -= Time.deltaTime;
 
             //toggle renderer
             GetComponent<Renderer>().enabled = !GetComponent<Renderer>().enabled;
 
             //wait for a bit
             yield return new WaitForSeconds(blinkTime);
         }
 
         //make sure renderer is enabled when we exit
         GetComponent<Renderer>().enabled = true;
         col.gameObject.SetActive (true);
     }
 }

I am not able to combine the two scripts without getting any errors :/ Any help would really be appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Taxen0 · Feb 05, 2016 at 07:54 PM

I haven't actually tried to do something like this from the OnCollisionEnter method before, but give this a try and tell me if you run into any issues. (untested code)

  using UnityEngine;
  using System.Collections;
  
  public class Bullet: MonoBehaviour {

      IEnumerator OnCollisionEnter (Collision col)
      {
          if(col.gameObject.tag == "RemovableObject")
          {
              col.gameObject.SetActive (false);
              yield return StartCoroutine(DoBlinks(3f, 0.2f, col.gameObject));
              col.gameObject.SetActive (true);
          }
      }

     IEnumerator DoBlinks(float duration, float blinkTime, GameObject target) 
      {
          while (duration > 0f) 
          {
              duration -= Time.deltaTime;
 
              //toggle renderer
              target.GetComponent<Renderer>().enabled = !target.GetComponent<Renderer>().enabled;
 
              //wait for a bit
              yield return new WaitForSeconds(blinkTime);
          }
 
      //make sure renderer is enabled when we exit
      target.GetComponent<Renderer>().enabled = true;
      target.gameObject.SetActive (true);
      }
 }
Comment
Add comment · Show 4 · 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 YoloJoe · Feb 05, 2016 at 09:52 PM 0
Share

It returns an error on line 31: The name `col' does not exist in the current context..

avatar image YoloJoe · Feb 05, 2016 at 09:59 PM 0
Share

Never $$anonymous$$d, It works, I just removed line 31. But I had this script attached to the bullet and not the object I wanted to blink. Thanks

avatar image YoloJoe · Feb 05, 2016 at 10:04 PM 0
Share

Wait no, this is wrong! The gameObject is not being disabled, the bullet is! :P It has to be the other way around. This script has to be attached to the BulletPrefab and deactivate what it hits.

avatar image Taxen0 YoloJoe · Feb 06, 2016 at 01:27 PM 0
Share

I see that i made some mistakes, I have updated the script so make another go. and yes, this is meant to be on the bullet (but should deactivate the correct target now)

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

40 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

Related Questions

how can I get my prefab to have a different material on instantiation 1 Answer

2 variables holding Renderer.materials change at the same time... 0 Answers

Script able to target two cameras? 0 Answers

the image set on the inspector is not displayed. I am a beginner 2 Answers

Universal Render Pipeline Compilation Error 7.2.1 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