UNITY 2D C#
Staram się wyzerować, na kilka sekund, prędkość rotacji w moich obiektach klonach, za każdym razem kiedy wcisnę przycisk, który dodałem do ekranu głównego mojej sceny w Unity. Uważam, że najlepszym rozwiązanie byłoby, aby wyszukać każdy obiekt z określonym tagiem (wszystkie klony mają ten sam tag). Problem polega na tym, że nie działa to w przypadku klonów...
Każdy klon ma dodany skrypt rotacji:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour {
public float speed;
public float speed2 = 0f;
private Rigidbody2D rb2D;
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void Update () {
transform.Rotate (0, 0, speed);
}
public void Stop (){
StartCoroutine(SpeedZero());
Debug.Log ("ZEROOOOO");
}
IEnumerator SpeedZero()
{
transform.Rotate (0, 0, speed2);
yield return new WaitForSeconds(20);
transform.Rotate (0, 0, speed);
}
}
Obiekty spawnuję za pomocą skryptu SunSpawner:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SunSpawner : MonoBehaviour {
public GameObject[] theSuns;
public Transform generationPoint;
private int sunSelector;
private float sceneHeight;
float distance = 0.5f;
Vector3 maxWidthPoint;
Vector3 minWidthPoint;
//Radious base on circle collider radious
float lastSunRadious = 2f;
void Start (){
minWidthPoint = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height / 2f));
maxWidthPoint = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height / 2f));
float bottom = Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y;
float top = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y;
sceneHeight = top - bottom;
Debug.LogFormat("Scene height: {0}, minWidthPoint: {1}, maxWidthPoint {2}", sceneHeight, minWidthPoint, maxWidthPoint);
}
public void Update (){
if (transform.position.y < generationPoint.position.y) {
sunSelector = Random.Range(0, theSuns.Length);
float currentSunRadious = theSuns[sunSelector].GetComponentInChildren<CircleCollider2D>().radius * theSuns[sunSelector].GetComponentInChildren<CircleCollider2D>().transform.localScale.x * theSuns[sunSelector].transform.localScale.x;
float distanceBetween = distance + lastSunRadious + currentSunRadious; //Random.Range(lastSunRadious+currentSunRadious, sceneHeight);
float sunXPos = Random.Range(minWidthPoint.x + currentSunRadious, maxWidthPoint.x - currentSunRadious);
Vector3 newSunPosition = new Vector3(sunXPos, transform.position.y + distanceBetween, transform.position.z);
transform.position = newSunPosition;
lastSunRadious = currentSunRadious;
Instantiate (theSuns [sunSelector], transform.position, transform.rotation);
}
}
}
Teraz dodałem Canvas i przycisk, w którym chciałem za pomocą funkcji OnClick zatrzymać rotację w każdym obiekcie (klonie), który istnieje a ma określony tag (w moim przypadku jest to "Log"). Jak już napisałem, nie potrafię tego jednak zrobić. Próbowałem kilku rzeczy, stworzyć listę, wyszukać tagu, odwołać się do skryptu Rotation i uruchomić Coroutine, ale nic nie chce działać. Aktualnie nie posiadam żadnego skryptu dodanego do przycisku stop, bo kompletnie już nie wiem, co tu zrobić
Jest ktoś w stanie mi pomóc?
Będę wdzięczny
Pozdrawiam