Array of a class

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.

using UnityEngine;
using System.Collections;

public class randomInit : MonoBehaviour 
{
	public GameObject prefab2;
	public GameObject currentPos;
	public float speed = 10f;
	public Transform pointA;
	public Transform pointB;
	public bool moving = true;

	

	IEnumerator waitForAChange()
	{
		float y = Random.Range(5f,10f);
		yield return new WaitForSeconds(y);
		moving = false;
	}

	IEnumerator waitForAChange2()
	{
		float y = Random.Range(5f,10f);
		yield return new WaitForSeconds(y);
		moving = true;
	}

	void changePosition()
	{
		if(moving)
		{

			float step = speed * Time.deltaTime;
			transform.position = Vector3.Lerp(transform.position,pointA.position,step);
			StartCoroutine(waitForAChange());


		}
		if(!moving)
		{
			float step = speed * Time.deltaTime;
			transform.position = Vector3.Lerp(transform.position,pointB.position,step);
			StartCoroutine(waitForAChange2());

		}
	}



	void Update()
	{
		changePosition();
		Destroy(GameObject.FindGameObjectWithTag("Respawn"),15f);
		//prefab = GameObject.FindGameObjectsWithTag("Respawn");
		//Destroy(prefab,2f);
	
	}
}

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:

using UnityEngine;
using System.Collections;



public class arrayOfarray : MonoBehaviour 
{
	/*changes the position of certain obstacles*/
	[System.Serializable]
	public class movingObstacles
	{
		public GameObject toMove;
		public Transform point01;
		public Transform point02;
		public bool Moving = false;
		public float speedMove;
		//public GameObject prefabObstacle;
		
	}
	
	public movingObstacles[] onMove;
	
	IEnumerator waitForAChange()
	{
		var equis2 = new movingObstacles();
		float y = Random.Range(5f,10f);
		yield return new WaitForSeconds(y);
		Debug.Log("seconds : " + y);
		equis2.Moving = false;
	}
	
	IEnumerator waitForAChange2()
	{
		var equis3 = new movingObstacles();
		float y = Random.Range(5f,10f);
		yield return new WaitForSeconds(y);
		Debug.Log("seconds : " + y);
		equis3.Moving = true;
	}
	
	void changePosition()
	{
		var equis = new movingObstacles();
		
		for(int i = 0; i < onMove.Length; i++)
		{
			if(equis.Moving)
			{
				equis.Moving = true;
				float step = equis.speedMove * Time.deltaTime;
				equis.toMove.transform.position = Vector3.Lerp(transform.position,equis.point01.position,step);
				StartCoroutine(waitForAChange());
				Debug.Log("moving");
								
			}
			if(!equis.Moving)
			{
				equis.Moving = false;
				float step = equis.speedMove * Time.deltaTime;
				transform.position = Vector3.Lerp(transform.position,equis.point02.position,step);
				StartCoroutine(waitForAChange2());
				Debug.Log("not moving");
			}
		}
	}
	/*end of the snippet*/


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		changePosition();
	}
}

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.

Any thoughs?

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

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?

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.

I don’t fully understand what you’re trying to do with your code, but here goes nothing:

This doesn’t make sense:

	IEnumerator waitForAChange()
	{
		var equis2 = new movingObstacles();

Nor does this:

	IEnumerator waitForAChange2()
	{
		var equis3 = new movingObstacles();

Neither does this:

	void changePosition()
	{
		var equis = new movingObstacles();

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:

	IEnumerator waitForAChange(movingObstacles equis2)
	{
		float y = Random.Range(5f,10f);
		yield return new WaitForSeconds(y);
		Debug.Log("seconds : " + y);
		equis2.Moving = false;
	}

Do the same for waitForChange2.

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

	void changePosition()
	{
		for(int i = 0; i < onMove.Length; i++)
		{
                        var equis = onMove[i]
			if(equis.Moving)
			{
				equis.Moving = true;
				float step = equis.speedMove * Time.deltaTime;
				equis.toMove.transform.position = Vector3.Lerp(transform.position,equis.point01.position,step);
				StartCoroutine(waitForAChange(equis));
				Debug.Log("moving");
								
			}
			if(!equis.Moving)
			{
				equis.Moving = false;
				float step = equis.speedMove * Time.deltaTime;
				transform.position = Vector3.Lerp(transform.position,equis.point02.position,step);
				StartCoroutine(waitForAChange2(equis));
				Debug.Log("not moving");
			}
		}
	}
	/*end of the snippet*/

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.