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 ineteye · Oct 17, 2012 at 04:56 PM · c#

Need help converting js to C# -- yield return WaitForSeconds

Hi! Need help to convert to C#

JS code:

 function scoresCalculation()
 {
   yield WaitForSeconds (0.2);
 }
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
-1
Best Answer

Answer by ineteye · Oct 18, 2012 at 11:51 AM

Think i find the problem with object destroying.. but then how to move destroy to scoreCalculation ();

script clickdetecting.cs :

 public IEnumerator scoresCalculation ()
 {
     
     yield return new WaitForSeconds(0.2f);    
 
     Debug.Log("TEST");
     if(destroyed == 3)
     {//if we destroyed 3 ball
         GameObject score1=(GameObject)Instantiate(scoreParticle, obj - new Vector3(0,0,0.3f), Quaternion.identity); //instantiate scoreParticle
         score1.renderer.material=score30; //give material to scoreParticle
         score1.renderer.material.color = color;//give ball's color to scoreParticle
         //scoreText.text=""+(int.Parse(scoreText.text)+30); //plus 30 to score text 
         container.GetComponent<instantiate>().score=""+(int.Parse(container.GetComponent<instantiate>().score)+30); //plus 30 to score text 
     }
     //below this is everything same logic
     if(destroyed == 4)
     {
         GameObject score2=(GameObject)Instantiate(scoreParticle, obj - new Vector3(0,0,0.3f), Quaternion.identity);
         score2.renderer.material=score50;
         score2.renderer.material.color = color;
         //scoreText.text=""+(int.Parse(scoreText.text)+50);
         container.GetComponent<instantiate>().score=""+(int.Parse(container.GetComponent<instantiate>().score)+50);
 
     }
     print("destroyed "+destroyed);
     destroyed=0;
 
 }

other script chack.cs where score calculation called:

 using UnityEngine;
 using System.Collections;
 
 public class chack : MonoBehaviour
 {
 
 //this script is attached every ball and it must be disabled, clickdetecting script will enable this
     public GameObject particle;
     private bool  count = true;
     private int   count1 =0;
     private int   count2 =0;
 
     public void Start ()
     {
         
        GameObject container = GameObject.Find ("Container"); //container which contains ball's objects
        rigidbody.position = new Vector3 (rigidbody.position.x, rigidbody.position.y, 0);
        Vector3 myPos = transform.position; 
 
        foreach (Transform child in container.transform) { //check if there is same tagged ball near this ball  
          
          Transform t = child.transform; 
          t.position = new Vector3 (t.position.x, t.position.y, 0);
 
          myPos = new Vector3 (myPos.x, myPos.y, 0);
             
          var distance = (t.position - myPos).sqrMagnitude;     
 
         
          if (distance > 0.0001f && distance < 0.1f && gameObject.tag == child.gameObject.tag) 
           { //if distance is less then 0.1f between same balls
           count1++;
           //Debug.Log("distance = "+distance);
           //Debug.Log ("clickdetecting.destroyed = "+clickdetecting.destroyed+" count "+count);
           child.gameObject.GetComponent<chack>().enabled = true; //enable same ball's "chack" script
           Color[] modifiedColors = particle.GetComponent<ParticleAnimator>().colorAnimation; //get particle color and modify it to match this ball's color
           modifiedColors [0] = renderer.material.color;
           modifiedColors [1] = renderer.material.color;
           modifiedColors [2] = renderer.material.color;
           modifiedColors [3] = renderer.material.color;
           modifiedColors [4] = renderer.material.color;
           particle.GetComponent<ParticleAnimator>().colorAnimation = modifiedColors; //give particle modified color
           var newParticle = Instantiate (particle, transform.position, transform.rotation); //instantiate particle        
           
                 
         if (count) {
             clickdetecting.destroyed++;
               //if (Time.timeScale != 0 && count1==count2) 
             if (Time.timeScale != 0) 
             {
                 StartCoroutine(Camera.main.GetComponent<clickdetecting>().scoresCalculation ());//call clickdetecting script which is attached to main camera
             }
               count = false;
           }
 
           Destroy (gameObject); // Think that is problemed Destroy ....
          }
        }
 
 
     }
 
 }
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 Seth-Bergman · Oct 18, 2012 at 11:54 AM 0
Share

don't post comments as answers please

avatar image ineteye · Oct 18, 2012 at 12:02 PM 0
Share

O$$anonymous$$.. but how should i post a code? it did not format correct in commnets..

avatar image Seth-Bergman · Oct 18, 2012 at 12:05 PM 0
Share

pretty sure you're able to edit the original post, no?

avatar image ineteye · Oct 18, 2012 at 12:06 PM 0
Share

ok i understand..

avatar image Seth-Bergman · Oct 18, 2012 at 12:19 PM 0
Share

ok.. bottom line, yes that is your issue, you are destroying your object I$$anonymous$$$$anonymous$$EDIATELY after calling the score method, so the script is immediately destroyed, no yield ever has time to run for even a single frame, let alone .2 seconds. When you destroy the object, you destroy the script on the object, so the co-routine never finishes. How to fix? Don't destroy the object until the code you want to finish has finished.. there are numerous ways to accomplish this.. but mainly you just need to move the Destroy line to the end of the other script:

 ...
 }
 print("destroyed "+destroyed);
 destroyed=0;
 Destroy(gameObject);
 ...
Show more comments
avatar image
1

Answer by Seth-Bergman · Oct 17, 2012 at 05:55 PM

Have a look in the script reference:

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

notice its examples are provided in either language.. In this case, you need to make sure your method has a return type of IEnumerator (and include the "return new" keywords):

 using UnityEngine;
 using System.Collections;
 
 public class scores : MonoBehaviour {
     IEnumerator scoresCalculation() {
         yield return new WaitForSeconds(.2);
     }
 }

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 ineteye · Oct 18, 2012 at 07:16 AM 0
Share

So such solution will work.. it just thinking that it did not work:

public void scoresCalculation () { Wait();

if (..)... { } }

IEnumerator Wait() { yield return new WaitForSeconds(0.2f); }

avatar image whydoidoit · Oct 18, 2012 at 07:22 AM 0
Share

No that won't work. You can't just delay a non-coroutine like that.

avatar image ineteye · Oct 18, 2012 at 07:24 AM 0
Share

So i have to create new file with new class?

avatar image whydoidoit · Oct 18, 2012 at 07:24 AM 0
Share

When you want to delay something you need the delayed things to also be in the coroutine. Like @Fattie says there's good examples of how to get things to happen later on http://unitygems.com/mistakes1 - coroutines can be complicated when you haven't used them, things like this don't really need them.

avatar image whydoidoit · Oct 18, 2012 at 07:27 AM 0
Share

No - the routine with your wait, waits but then does nothing. You'd need the delayed thing to be in the same function as the WaitForSeconds. Seriously though, use Invoke.

Show more comments
avatar image
-2

Answer by ineteye · Oct 18, 2012 at 10:33 AM

So this on JS:

 function Destroying(){
 for (var child:Transform in container.transform){
 Destroy(child.gameObject); //destroy the ball
         var modifiedColors : Color[] = particle.GetComponent.<ParticleAnimator>().colorAnimation; //get particle color and modify it to match this ball's color
         modifiedColors[0] = child.renderer.material.color;
         modifiedColors[1] = child.renderer.material.color;
         modifiedColors[2] = child.renderer.material.color;
         modifiedColors[3] = child.renderer.material.color;
         modifiedColors[4] = child.renderer.material.color;
         particle.GetComponent.<ParticleAnimator>().colorAnimation = modifiedColors; //give particle modified color
         var particle=Instantiate(particle, child.transform.position, child.transform.rotation) as GameObject; //instantiate particle
 yield WaitForSeconds(1.5); //wait 0.5 second 
 }
 enabled=false;
 }


Will be in C#:

 bool sleep;
 
 public void Destroying (){
 foreach(Transform child in container.transform){
 Destroy(child.gameObject); //destroy the ball
         Color[] modifiedColors = particle.GetComponent<ParticleAnimator>().colorAnimation; //get particle color and modify it to match this ball's color
         modifiedColors[0] = child.renderer.material.color;
         modifiedColors[1] = child.renderer.material.color;
         modifiedColors[2] = child.renderer.material.color;
         modifiedColors[3] = child.renderer.material.color;
         modifiedColors[4] = child.renderer.material.color;
         particle.GetComponent<ParticleAnimator>().colorAnimation = modifiedColors; //give particle modified color
         GameObject NewParticle=Instantiate(particle, child.transform.position, child.transform.rotation) as GameObject; //instantiate particle
 //yield return new WaitForSeconds(1.5f); //wait 0.5f second 
 if(sleep) return;
 StartCoroutine(Wait());
 }
 enabled=false;
 }
     
     IEnumerator Wait()
 {
     sleep=true;
     yield return new WaitForSeconds(1.5f);    
 }
Comment
Add comment · Show 3 · 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 Seth-Bergman · Oct 18, 2012 at 10:43 AM 0
Share

no, that won't work... but just make the return type of your method IEnumerator, and yield should work:

 public IEnumerator Destroying (){
 foreach(Transform child in container.transform){
 Destroy(child.gameObject); //destroy the ball
        Color[] modifiedColors = particle.GetComponent<ParticleAnimator>().colorAnimation; //get particle color and modify it to match this ball's color
        modifiedColors[0] = child.renderer.material.color;
        modifiedColors[1] = child.renderer.material.color;
        modifiedColors[2] = child.renderer.material.color;
        modifiedColors[3] = child.renderer.material.color;
        modifiedColors[4] = child.renderer.material.color;
        particle.GetComponent<ParticleAnimator>().colorAnimation = modifiedColors; //give particle modified color
        GameObject NewParticle=Instantiate(particle, child.transform.position, child.transform.rotation) as GameObject; //instantiate particle
 yield return new WaitForSeconds(1.5f); //wait 0.5f second 
 
 }
 enabled=false;
 }
avatar image ineteye · Oct 18, 2012 at 10:58 AM 0
Share

So simalar script later still did not do the same...

avatar image ineteye · Oct 18, 2012 at 11:02 AM 0
Share

Oh so i should call those script in way describe here: http://unitygems.com/mistakes1/#die ???

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

11 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

Related Questions

Enemy AI script causing Unity to crash (Javascript to C#) 1 Answer

Need help converting js to C 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

JS to C#, anyone? 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