Search Unity

[SOLVED] How to stop rotation in clones?

Discussion in 'Scripting' started by polan31, Jul 16, 2019.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    142
    I have a few prefabs. Each object has a script that allows it to rotate. I would like to be able to stop rotations with the added button (OnClick). I tried to solve this with the tag. I created a button to which I added a script with FindGameObjectsWithTag. 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?

    Script responsible for rotation (added to each prefab object):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Rotation : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public float speed2 = 0f;
    9.     private Rigidbody2D rb2D;
    10.  
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.      
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     public void Update () {
    20.  
    21.         transform.Rotate (0, 0, speed);
    22.     }
    23.  
    24.     public void Stop (){
    25.  
    26.         StartCoroutine(SpeedZero());
    27.     }
    28.  
    29.     IEnumerator SpeedZero()
    30.     {
    31.         transform.Rotate (0, 0, speed2);
    32.         yield return new WaitForSeconds(10);
    33.         transform.Rotate (0, 0, speed);
    34.  
    35.     }
    36. }
    37.  
    38.  
    Script responsible for spawning:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SunSpawner : MonoBehaviour {
    6.  
    7.  
    8.     public GameObject[] theSuns;
    9.     public Transform generationPoint;
    10.  
    11.  
    12.     private int sunSelector;
    13.     private float sceneHeight;
    14.  
    15.     float distance = 0.5f;
    16.  
    17.  
    18.  
    19.     Vector3 maxWidthPoint;
    20.     Vector3 minWidthPoint;
    21.  
    22.     //Radious base on circle collider radious
    23.     float lastSunRadious = 2f;
    24.  
    25.  
    26.  
    27.     void Start (){
    28.         minWidthPoint = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height / 2f));
    29.         maxWidthPoint = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height / 2f));
    30.  
    31.         float bottom = Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y;
    32.         float top = Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y;
    33.         sceneHeight = top - bottom;
    34.  
    35.         Debug.LogFormat("Scene height: {0}, minWidthPoint: {1}, maxWidthPoint {2}", sceneHeight, minWidthPoint, maxWidthPoint);
    36.  
    37.  
    38.     }
    39.  
    40.      
    41.  
    42.     public void Update (){
    43.  
    44.         if (transform.position.y < generationPoint.position.y) {
    45.  
    46.             sunSelector = Random.Range(0, theSuns.Length);
    47.             float currentSunRadious = theSuns[sunSelector].GetComponentInChildren<CircleCollider2D>().radius * theSuns[sunSelector].GetComponentInChildren<CircleCollider2D>().transform.localScale.x * theSuns[sunSelector].transform.localScale.x;
    48.             float distanceBetween = distance + lastSunRadious + currentSunRadious; //Random.Range(lastSunRadious+currentSunRadious, sceneHeight);
    49.             float sunXPos = Random.Range(minWidthPoint.x + currentSunRadious, maxWidthPoint.x - currentSunRadious);
    50.  
    51.             Vector3 newSunPosition = new Vector3(sunXPos, transform.position.y + distanceBetween, transform.position.z);
    52.  
    53.             transform.position = newSunPosition;
    54.             lastSunRadious = currentSunRadious;
    55.             Instantiate (theSuns [sunSelector], transform.position, transform.rotation);
    56.  
    57.  
    58.         }
    59.     }
    60. }
    61.  
    62.  
     
  2. mountblanc

    mountblanc

    Joined:
    Sep 24, 2015
    Posts:
    72
    I have not much time but if i read this i think you need to set the tag of the instantiate:
    quick example:
    Code (CSharp):
    1. var obj = Instantiate (theSuns [sunSelector], transform.position, transform.rotation);
    2. obj.tag = "#YourTag#";
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    We'd need to see the code where you use FindGameObjectWithTag to know what's going wrong with that.
     
  4. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    142
    Something simple :


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class StopRotationBTN : MonoBehaviour
    {
    public Rotation other;

    GameObject obj;

    // Start is called before the first frame update
    void Start()
    {

    obj = GameObject.FindWithTag("Log");

    }

    // Update is called once per frame
    public void OnClick() {
    other.Stop ();
    }
    }



    I am trying to get access to functions in the Rotation script for each object with the tag Log.
    Unfortunately, I only have access to the first object that is not a clone. For the rest, which are clones, it doesn't work.
     
  5. mountblanc

    mountblanc

    Joined:
    Sep 24, 2015
    Posts:
    72
    If you do it like this It takes the first gameobject in the scene with that tag.
    And even then you do not have excess to the class (but i think you left some of your code out)
    a quick possible solution would be:
    Code (CSharp):
    1.  
    2.         public class StopRotationBTN : MonoBehaviour
    3.         {
    4.             private SunSpawner[] m_aSunSpawners;
    5.  
    6.             public Start()
    7.             {
    8.                 var aItems = GameObject.FindGameObjectsWithTag("Log");
    9.  
    10.                 int nSize = aItems.Length;
    11.  
    12.                 m_aSunSpawners= new SunSpawner[nSize];
    13.  
    14.                 for (int t = 0; t < nSize; t++)
    15.                 {
    16.                     m_aSunSpawners[t] = aItems[t].GetComponent<SunSpawner>();
    17.                 }
    18.             }
    19.             public void OnClick()
    20.             {
    21.                 int nSize = m_aSunSpawners.Length;
    22.                 for (int t = 0; t < nSize; t++)
    23.                 {
    24.                     m_aSunSPawners[t].Stop();
    25.                 }
    26.             }
    27.         }
    28.  
    29.  
    p.s. I hope i didnt make any typo's