- Home /
Question by
Lemo76 · Feb 18, 2015 at 06:45 AM ·
javascriptgameobjectarraycontains
Copying Certain Data between two Arrays
So I've got an array:
private var SceneObjects : GameObject [];
This has all of the objects in the scene. Now, I've got this other array:
private var Platforms : GameObject [];
How do I set the array Platforms to have only the gameobjects in SceneObjects which contain the name 'PlatCenter'?
for (var Plats : GameObject in SceneObjects) {
if (Plats.gameObject.name.Contains("PlatCenter")) {
for (var i : int = 0; i<SceneObjects.length; i++){
Platforms[i] = SceneObjects[i];
}
}
}
I've tried that, but it is only producing the error: NullReferenceException: Object reference not set to an instance of an object - (wrapper stelemref) object:stelemref (object,intptr,object)
Thanks.
Comment
Answer by zharik86 · Feb 18, 2015 at 07:05 AM
First, you must declare your array in memory. Second, in your search code you don't know, how many objects with name "PlatCenter" on your Scene. Than I recommend using List. See example below:
import System.Collections.Generic;
public var SceneObjects : GameObject [];
var platformsList : List.<GameObject> = new List.<GameObject>();
function Start() {
this.mySearch();
}
function mySearch() {
for (var Plats : GameObject in SceneObjects) {
//Name is contains "PlatCenter", than add to list
if (Plats.name.Contains("PlatCenter")) {
platformsList.Add(Plats);
}
}
}
I hope that it will help you.