Script effects all gameobjects.

Hello Unity Community,

I’ve been working on static variables and array methods. Therefor I wrote a code piece which Works nice with one collider but it has problems when i add two…

Our Block Object’s script

//Object's color variable.
var objectColor : String;
//Object's Unique ID.
var objectID : String;

//At first...
function Start () {
	objectID = (RandomColorGenerator.selectedColor.ToString() + Spawning.spawnCount.ToString());
	objectColor = RandomColorGenerator.selectedColor;
	gameObject.GetComponent(SpriteRenderer).color = RandomColorGenerator.color; = objectID;


function OnTriggerEnter2D(rowCollider : Collider2D) {

	var blocksIdArray = rowCollider.gameObject.GetComponent(RowScript).blocksIdArray;
	var blocksColorArray = rowCollider.gameObject.GetComponent(RowScript).blocksColorArray;
	if(rowCollider.gameObject.tag == "Row") {


and our row’s array script

//Variable for blocks ID array
public static var blocksIdArray = Array();
//Variable for observing.
var blocksIdArrayInspector  : String[];

//Variable for blocks color array
public static var blocksColorArray = Array();
//Variable for observing.
var blocksColorArrayInspector  : String[];

function Start() {



function Update() {

	blocksColorArrayInspector = blocksColorArray;
	blocksIdArrayInspector = blocksIdArray;


now when i do this;

My Cyan block collides with collider 6, not 7 but, they have same script. Why this problem happens ? Does anyone have any idea ?

I think youngapprentice got it right about your use of static variables here.

public static var blocksIdArray = Array();

This is defining your data structure for each row, but it’s global, so every row defining that overwrites it for the other rows each time. Whichever row loads that data last is the only one storing data in that variable.


Here, you’re using said variable. You’re loading up data in OnTriggerEnter for the region entered, but the data it calls doesn’t vary per row.

You’ll want to change back away from static variables in this situation, because you DON’T want them to be the same for everything.