I have a few prefabs. Each object has a script that allows it to rotate (Script "Rotation"). I try to stop, for a few seconds, the rotation speed in my clone objects, every time when I press a button that I added to the main screen of my scene in Unity. think the best solution would be to find every object with a specific tag (all clones have the same tag). After pressing the button, each object with the specified tag should stop. Unfortunately, it doesn't work on objects that are clones... Can anyone solve my problem?
Each clone has a rotation script:
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);
}
}
The objects are spawned using the SunSpawner script:
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;
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);
}
}
}
Now I added Canvas and a button in which I wanted to use the OnClick function to stop rotation in every object (clone) that exists and has a specific tag (in my case it is "Log"). As I wrote, I can't do it. I tried a few things, create a list, search for a tag, refer to the Rotation script and run coroutine, but nothing works. Currently, I don't have any script added to the stop button, because I don't know how to solve it.