Using an array to get scripts

Hi i have these blocks and when you click on them i use this code to add them to an array in a different script.

if(Input.GetMouseButton(0)  collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,Mathf.Infinity)  selected == false) {
    	manager.addToArray(this.gameObject);
    }

// this calls this function in the manager object
function addToArray (obj : GameObject) {
  myNumbers += [obj];
}

Then in the manager i use a for loop to go through all the objects you have clicked on once you lift up your mouse button.

var script : number_block_script[];
for(var x = 0; x < myNumbers.length; x++){
			script[x] = myNumbers[x].GetComponent(number_block_script);
			script[x].test(); // i want to be able to call functions in the number blocks that you have clicked and not do anything 
                                              // to the ones you have not clicked.
		}

whenever i run this i get an error saying Array index is out of range. I have tried a lot of other things but so far nothing has worked and i’ve run out of ideas. If anyone knows a solution please let me know.

Is script the same size as myNumbers?

I think this is a logic mistake.
You change ‘myNumbers’ array dynamicly in addToArray function, and its lenght will increase everytime when you click the mouse (if you click on object 1000 times, then myNumbers.length will be equals 1000). And ‘script’ variable have to have the same length, but it looks like you set this variable in Inspector panel.

Are you sure they have the same size? :face_with_spiral_eyes:

You created a blank array and attempted to access it. When you create script, it has a size of 0 and at the next line, you created a for-loop which accessed the blank array, hence giving you an array index out of range because script array has a size of 0.

What you want to do is

var script : number_block_script[] = new number_block_script[myNumbers.Length]; // Create the array of the same size as your other array
for(var x = 0; x < myNumbers.length; x++){
            script[x] = myNumbers[x].GetComponent(number_block_script);
            script[x].test();
        }

You could just use Lists with foreach statements to go trough the elements. Use list.Add(element) and list.RemoveAt(indexOfElement)

Yes, I have this issue. My issue being that, each script has a different name, so, in an array of potentialScriptName, how does one insure item I, can call anyScriptName? Are lists the solution? Dictionaries? Unsure.

use an abstract class from which u extend all the other classes, then you can call the override methods in your respective classes.

//base class
public abstract class BaseClass : MonoBehaviour {
	protected string name;
	
	public abstract void Action1();
	public abstract void Action2();
	public abstract void StopActions();
}

//derived class
public class Script1 : BaseClass {
	public override void Action1()
	{
		//do stuff
	}
	public override void Action2()
	{
		//do stuff
	}
	public override void Action1()
	{
		//do stuff
	}
	public override void StopActions()
	{
		//do stuff
	}

	void Start(){
		name = "script1";
	}
}

//then use a List or an array of BaseClass type to store your scripts, and call those 3 
//methods or whatever methods that are in the BaseClass. and they'll get called from 
//your derived classes.

Thanks.
I will have to look into more.

I love you that fixed it :smile: