- Home /
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;
gameObject.name = objectID;
}
function OnTriggerEnter2D(rowCollider : Collider2D) {
var blocksIdArray = rowCollider.gameObject.GetComponent(RowScript).blocksIdArray;
var blocksColorArray = rowCollider.gameObject.GetComponent(RowScript).blocksColorArray;
if(rowCollider.gameObject.tag == "Row") {
rowCollider.gameObject.GetComponent(RowScript).blocksIdArray.Push(objectID);
rowCollider.gameObject.GetComponent(RowScript).blocksColorArray.Push(objectColor);
}
}
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() {
blocksColorArray.Clear();
blocksIdArray.Clear();
}
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 ?
Try and fix up your code formatting. Also, I don't have time right now to read all of this, but you mentioned static variables. If you have a script affecting all objects, then if they all depend on that one static variable, when you change that variable it will change on all objects. This can cause some hairy situations.
Well actually all rows uses same script. But i want to change only collided row's script variable. Isn't it possible ?
What are you referring to when you say collider 6 and collider 7?
Answer by Eno-Khaon · Apr 26, 2015 at 07:09 PM
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.
rowCollider.gameObject.GetComponent(RowScript).blocksIdArray.Push(objectID);
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.
That's exactly what I want. But if static variables applies to every code piece, what should i use ?
You'd simply need a non-static variable. When each row generates its properties, they can be stored in a public variable. You'll need to have a script variable and use GetComponent(RowScript) to access them, but that's simply the way to separate them into their own entities.
function OnTriggerEnter2D(rowCollider : Collider2D) {
var blocksIdScript = rowCollider.gameObject.GetComponent(RowScript);
var blocksIdArray = blocksIdScript.blocksIdArray;
var blocksColorArray = blocksIdScript.blocksColorArray;
if(rowCollider.gameObject.tag == "Row") {
blocksIdScript.blocksIdArray.Push(objectID);
blocksIdScript.blocksColorArray.Push(objectColor);
}
}
You're already accessing the other script through the object rather than accessing the static variables globally, so you wouldn't even really need to change anything there. I would recommend loading the script into its own variable, however, so you're not re-requesting the script four times at a time.
Well it seems i need to write another algorithm... Thanks by the way.