- Home /
Need help C# -- yield, destroy
I need help to destroy object, but allow to work scoresCalculation() correctly... Now - Destroy (gameObject); - at the end of chack.cs script... detroy ealier that it should... best way will be move Destroy to the end of clickdetecting.cs, but the problem, that chack.cs and clickdetecting.cs added to different objects and chack.cs is added to clone of prefab object... and it more then one object...
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 ....
}
}
}
}
Answer by whydoidoit · Oct 18, 2012 at 06:24 PM
Ok so see this article. You are starting a coroutine on the local object then destroying it. You should start it on the Camera.main...
Hi! Thanks but i read those article... and my problem did not describe there... i do not know how to destroy object... if code in separated scripts...
Yeah I just updated it to point out the thing about the local object I mentioned in my comment.
var cd = Camera.main.GetComponent<clickdetecting>();
cd.StartCoroutine(cd.scoresCalculation());
you mean replace :
StartCoroutine(Camera.main.GetComponent().scoresCalculation ());
with
var cd = Camera.main.GetComponent(); cd.StartCoroutine(cd.scoresCalculation());
But what benefit to change this line??
Per the article - you are calling Destroy on the object you start the coroutine on - so it won't run.
Your answer
Follow this Question
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
Need help converting js to C# -- yield return WaitForSeconds 3 Answers