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 dylan760 · Jul 11, 2014 at 12:02 PM · collisionerrorwaitforsecondscolour

im trying to make it if a specific item collides with a cube it flashes red but it keeps comming up with an error

on unity i am trying to make it if a specific item collides with a cube it flashes red but it keeps comming up with an error

; expected or = (cannot specify constructor arguments in declaration)

and it highlights the "yield WaitForSeconds(0.5);" row can somebody tell me whats wrong?

this is the code

 using UnityEngine; 
 using System.Collections; 
 
 public class swordCollideWithEnemybasic : MonoBehaviour { 
 
 private int enehealth = 0; 
 
 // Use this for initialization 
 void Start () { 
 
 } 
 
 // Update is called once per frame 
 void OnCollisionEnter (Collision hit) { 
 if (enehealth <= 0) 
 if (hit.gameObject.name == "sword") 
 gameObject.renderer.material.color = Color.red; 
 yield WaitForSeconds(0.5); 
 gameObject.renderer.material.color = Color.grey; 
 } 

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 dylan760 · Jul 11, 2014 at 10:24 AM 0
Share

formatted :)

avatar image Josh Naylor ♦♦ · Jul 11, 2014 at 12:02 PM 0
Share

http://docs.unity3d.com/ScriptReference/WaitForSeconds.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Razacx · Jul 11, 2014 at 12:21 PM

It is impossible to call a yield outside of a co-routine to my knowledge. You might want to remove the following code from OnCollisionEnter and put in in a coroutine:

 yield WaitForSeconds(0.5); 
 gameObject.renderer.material.color = Color.grey; 

Also, yield WaitForSeconds() is javascript syntax, in c# it would be done as the following:

 yield return new WaitForSeconds();

I also think you have made some mistakes with the parentheses of the if-statements in OnCollisionEnter.

Using all of this information, you should end up with something like this:

 using UnityEngine; 
 using System.Collections; 
  
 public class swordCollideWithEnemybasic : MonoBehaviour { 
  
 private int enehealth = 0; 
  
 // Update is called once per frame 
 void OnCollisionEnter (Collision hit) { 
   if (enehealth <= 0) { 
     if (hit.gameObject.name == "sword") {
       gameObject.renderer.material.color = Color.red;
       StartCoroutine(turnGreyDelayed());
     }
   }
 }
 
 //Coroutine for setting color back to grey after 0.5 seconds
 IENumerator turnGreyDelayed() {
   yield return new WaitForSeconds(0.5f); 
   gameObject.renderer.material.color = Color.grey; 
 }
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 dylan760 · Jul 11, 2014 at 01:10 PM 0
Share

Thank you so much!!!!!!!

avatar image
0

Answer by Landern · Jul 11, 2014 at 12:25 PM

As @josh-naylor stated and is indicating with his comment, your OnCollisionEnter method is mal-formed, and i'm also going to say your if statements are not blocked correctly. If you don't use curly braces, an if statement will only execute the next line of code if it the expression is true.

 using UnityEngine; 
 using System.Collections; 
  
 public class swordCollideWithEnemybasic : MonoBehaviour { 
  
 private int enehealth = 0; 
  
 // Use this for initialization 
 void Start () { 
  
 } 
  
 // Update is called once per frame 
 IEnumerator OnCollisionEnter (Collision hit) { 
   if (enehealth <= 0) {
     if (hit.gameObject.name == "sword") {
       gameObject.renderer.material.color = Color.red; 
       yield return new WaitForSeconds(0.5); 
       gameObject.renderer.material.color = Color.grey;
     }
   }
 } 

The method in c# yielding the waitforseconds needs to return IEnumerator/IEnumerable.

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 dylan760 · Jul 11, 2014 at 12:52 PM 0
Share

oh they are in my code i just forgot to paste them

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

6 People are following this question.

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

Related Questions

"Edit Collider" Button Gone 1 Answer

error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected 3 Answers

Display 1 No cameras rendering when player collides with enemy 1 Answer

Collision bug. 2 Answers

How to get the first person controller to collide with an animated object? 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