How to get access to the clone object?
2D c#
I have a few prefabs that generate clones. I added a rotation script to each object. I would like to add a button to the scene, with the help of which I will be able to Start Coroutine into every clone's object with specific tag. How to do it? A normal search a tag or the use of the option "other. (Function name)" doesn't work. Can someone help me solve this problem?
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);
}
}
Spawn 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;
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);
}
}
}
Answer by pyramidhead01 · Jul 16, 2019 at 09:00 PM
Let me know if this makes sense.
Make a variable List of these types of enemies and put it in the script that the OnClick() method is in:
public List<GameObject> objects = new List<GameObject>();
Add them either in the inspector or in that spawn script you have:
transform.position = newSunPosition;
lastSunRadious = currentSunRadious;
Instantiate (theSuns [sunSelector], transform.position, transform.rotation);
objects.Add(theSuns[sunSelector];
Then on that OnClick() method, do something like this:
public void OnClick()
{
int roll = new int;
if (objects.Count > 1)
{
roll = Mathf.RoundToInt(Random.Range(0, objects.count)
objects[roll].SpeedZero();
return;
}
else if (objects.Count > 0)
{
objects[0].SpeedZero();
}
}
After I posted this, I realized I never answered your question! Sorry, I get distracted!
When do you need something to stop? Will a bool need to be true, a player standing inside/outside of something, will a certain value need to be 0 or 100, need to have something in inventor/solve puzzle?
It sounds like you are trying to stop a specific sun, maybe when a task in the game is completed I guess? This solution will stop a random sun of course, but without knowledge of when a sun would need to stop, I can't tell you how to reference that sun on the onclick method.
Your answer
Follow this Question
Related Questions
Scripts randomly getting disabled 0 Answers
2D Sprite rotation 180 degrees 0 Answers
How can I destroy a prefab clone? 1 Answer
2D top down shooter can only aim to the top right (mouse aim) 0 Answers
2d Character not rotating right 0 Answers