Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Array of a class

Discussion in 'Scripting' started by JaimeMM, May 9, 2014.

  1. JaimeMM

    JaimeMM

    Joined:
    Apr 21, 2014
    Posts:
    2
    Hey there! I've been working in a script that's intended to run in a for loop.

    While I've done a script that runs for a single gameObject, I can't make it to work for it to run in many gameObjects while mantaining an organized way.

    Below is the working script, working for a single gameObject.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class randomInit : MonoBehaviour
    5. {
    6.     public GameObject prefab2;
    7.     public GameObject currentPos;
    8.     public float speed = 10f;
    9.     public Transform pointA;
    10.     public Transform pointB;
    11.     public bool moving = true;
    12.  
    13.    
    14.  
    15.     IEnumerator waitForAChange()
    16.     {
    17.         float y = Random.Range(5f,10f);
    18.         yield return new WaitForSeconds(y);
    19.         moving = false;
    20.     }
    21.  
    22.     IEnumerator waitForAChange2()
    23.     {
    24.         float y = Random.Range(5f,10f);
    25.         yield return new WaitForSeconds(y);
    26.         moving = true;
    27.     }
    28.  
    29.     void changePosition()
    30.     {
    31.         if(moving)
    32.         {
    33.  
    34.             float step = speed * Time.deltaTime;
    35.             transform.position = Vector3.Lerp(transform.position,pointA.position,step);
    36.             StartCoroutine(waitForAChange());
    37.  
    38.  
    39.         }
    40.         if(!moving)
    41.         {
    42.             float step = speed * Time.deltaTime;
    43.             transform.position = Vector3.Lerp(transform.position,pointB.position,step);
    44.             StartCoroutine(waitForAChange2());
    45.  
    46.         }
    47.     }
    48.  
    49.  
    50.  
    51.     void Update()
    52.     {
    53.         changePosition();
    54.         Destroy(GameObject.FindGameObjectWithTag("Respawn"),15f);
    55.         //prefab = GameObject.FindGameObjectsWithTag("Respawn");
    56.         //Destroy(prefab,2f);
    57.    
    58.     }
    59. }
    60.  

    Then, I've tried to make another script that runs in a for loop, and still stays organized. What I want is that for each 'public GameObject toMove;' to move from point01 to point02 and viceversa, all of this inside the class MovingOstacles.

    Here's the code:


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class arrayOfarray : MonoBehaviour
    7. {
    8.     /*changes the position of certain obstacles*/
    9.     [System.Serializable]
    10.     public class movingObstacles
    11.     {
    12.         public GameObject toMove;
    13.         public Transform point01;
    14.         public Transform point02;
    15.         public bool Moving = false;
    16.         public float speedMove;
    17.         //public GameObject prefabObstacle;
    18.        
    19.     }
    20.    
    21.     public movingObstacles[] onMove;
    22.    
    23.     IEnumerator waitForAChange()
    24.     {
    25.         var equis2 = new movingObstacles();
    26.         float y = Random.Range(5f,10f);
    27.         yield return new WaitForSeconds(y);
    28.         Debug.Log("seconds : " + y);
    29.         equis2.Moving = false;
    30.     }
    31.    
    32.     IEnumerator waitForAChange2()
    33.     {
    34.         var equis3 = new movingObstacles();
    35.         float y = Random.Range(5f,10f);
    36.         yield return new WaitForSeconds(y);
    37.         Debug.Log("seconds : " + y);
    38.         equis3.Moving = true;
    39.     }
    40.    
    41.     void changePosition()
    42.     {
    43.         var equis = new movingObstacles();
    44.        
    45.         for(int i = 0; i < onMove.Length; i++)
    46.         {
    47.             if(equis.Moving)
    48.             {
    49.                 equis.Moving = true;
    50.                 float step = equis.speedMove * Time.deltaTime;
    51.                 equis.toMove.transform.position = Vector3.Lerp(transform.position,equis.point01.position,step);
    52.                 StartCoroutine(waitForAChange());
    53.                 Debug.Log("moving");
    54.                                
    55.             }
    56.             if(!equis.Moving)
    57.             {
    58.                 equis.Moving = false;
    59.                 float step = equis.speedMove * Time.deltaTime;
    60.                 transform.position = Vector3.Lerp(transform.position,equis.point02.position,step);
    61.                 StartCoroutine(waitForAChange2());
    62.                 Debug.Log("not moving");
    63.             }
    64.         }
    65.     }
    66.     /*end of the snippet*/
    67.  
    68.  
    69.     // Use this for initialization
    70.     void Start () {
    71.    
    72.     }
    73.    
    74.     // Update is called once per frame
    75.     void Update ()
    76.     {
    77.         changePosition();
    78.     }
    79. }

    And below is the screenshot that shows how each Element in the array has 5 more elements. I want the element ToMove to move between the elements point01 and point02 with a certain speed.

    $Sin título.png

    Any thoughs?

    ps- not a native english speaker, sorry for any mistakes.
     
  2. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    In the inspector, Is RPolenMoving a prefab in your scene or project that you want to instantiate at runtime? Or is it an already instantiated GameObject that you want to move around?
     
  3. JaimeMM

    JaimeMM

    Joined:
    Apr 21, 2014
    Posts:
    2
    Hey there!

    It's already an instantiated prefab, there are 3 gameObjects in the scene for the first Element in the array, which contains 5 more elements, the first element is ToMove - which contains the gameObject I want to move from point01 which is a transform to point02 which is another transform.

    The boolean Moving just represents if the gameObject is moving. And finally the variable SpeedMove represents how fast I want the gameObject ToMove to move.
     
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    I don't fully understand what you're trying to do with your code, but here goes nothing:

    This doesn't make sense:
    Code (csharp):
    1.     IEnumerator waitForAChange()
    2.     {
    3.         var equis2 = new movingObstacles();
    4.  
    Nor does this:
    Code (csharp):
    1.     IEnumerator waitForAChange2()
    2.     {
    3.         var equis3 = new movingObstacles();
    4.  
    Neither does this:
    Code (csharp):
    1.     void changePosition()
    2.     {
    3.         var equis = new movingObstacles();
    4.  
    The above statements will crate new instances of the movingObstacles class, while I'm guessing that what you want to do is access existing instances stored in the onMove array.


    If that is the case, then change waitForChange to accept the movingObstacles instance as a parameter, as follows:

    Code (csharp):
    1.     IEnumerator waitForAChange(movingObstacles equis2)
    2.     {
    3.         float y = Random.Range(5f,10f);
    4.         yield return new WaitForSeconds(y);
    5.         Debug.Log("seconds : " + y);
    6.         equis2.Moving = false;
    7.     }
    8.  
    Do the same for waitForChange2.

    Also change how you call the above two coroutines in the changePosition method.

    Code (csharp):
    1.     void changePosition()
    2.     {
    3.         for(int i = 0; i < onMove.Length; i++)
    4.         {
    5.                         var equis = onMove[i]
    6.             if(equis.Moving)
    7.             {
    8.                 equis.Moving = true;
    9.                 float step = equis.speedMove * Time.deltaTime;
    10.                 equis.toMove.transform.position = Vector3.Lerp(transform.position,equis.point01.position,step);
    11.                 StartCoroutine(waitForAChange(equis));
    12.                 Debug.Log("moving");
    13.                                
    14.             }
    15.             if(!equis.Moving)
    16.             {
    17.                 equis.Moving = false;
    18.                 float step = equis.speedMove * Time.deltaTime;
    19.                 transform.position = Vector3.Lerp(transform.position,equis.point02.position,step);
    20.                 StartCoroutine(waitForAChange2(equis));
    21.                 Debug.Log("not moving");
    22.             }
    23.         }
    24.     }
    25.     /*end of the snippet*/
    26.  
    You were creating instances of movingObstacles inside those three methods without connecting them to the existing instances you have in the onMove array. So it's expected that changing those instances will have no effect on the GameObject s stored in onMove.

    Not sure if that helps but it's what I understood from you code.