- Home /
How to create dynamic array of type GameObject using javascript?
Hi everyone, i have just started the work before a week in unity3d to make my first game. I'm using javascript. i know my question is asked so many time.
My concept is kick a ball to selected target like penalty shoot out. I tried so many references to solve my problem but bad luck :(
I have a fixed number of targets i.e. 6. and created an array of target in the script.
var targets : GameObject[];
and filling array in start() by
targets = GameObject.FindGameObjectsWithTag("Target");
Also removing/destroying the target object when ball hits target
function OnTriggerEnter(collider : Collider)
{
if (collider.transform.name == "Ball")
{
Destroy(gameObject);
}
}
My problem is that when i select the another target it's giving me following error.
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
i think my array is not dynamic. I'm also not able to add/remove any object runtime from array. how can i do it? Please suggest me. Please provide me some detail what i'm doing wrong?
Answer by Kiloblargh · Jan 22, 2014 at 07:36 AM
A "dynamic array" is called a List.
But that isn't your problem here. Your problem is probably elsewhere in code that you didn't post; where you select a target, you're not selecting the target you think you are selecting.
You can say for example "` targets[4] = null;`", or "`targets [2] = hit.transform.gameObject`" and it will work.
Also, you can not destroy the game object, but instead use SetActive (false).
This will get rid of the MissingReferenceException compile error but not the design error that caused it.
Thanks a lot for reply. I don't know how to use 'List' in javascript.
I'm selecting the target in function On$$anonymous$$ouseDown()
function On$$anonymous$$ouseDown()
{
for(var i : int = 0; i < target.Length; i++)
{
if(transform.name == targets[i].name)
{
Debug.Log("Clicked Target index "+i+" is named "+targets[i].name);
foundTargetIndex=i;
currentSelectedTarget=i;
targetFound=true;
}
}
//and below loop to hide all unselected target
}
When game is load first time, and i select one target, for e.g. 1st target is selected then i'm showing only that target and hiding other targets. After this step ball is shoot and if it's collide with selected target then i'm destroying target.
I'm not removing that object from array. why i can't do it?
Answer by ragnaros100 · Jan 22, 2014 at 07:51 AM
I'm not sure if it helps, but I know that in c# you can make a List instead of an array, which is more or less dynamic.
However. This doesnt really seem to be your problem. It seems you're trying to edit or read some data from your array of gameObjects with the tag "Targets" and you've deleted some of the targets from the scene. The script attempts to access a destroyed object, which is no longer there. This can be overcome by making a new function that is run whenever the object gets destroyed. It should look something like this:
function dyingTarget(GameObject dyingObject)
{
targets = GameObject.FindGameObjectsWithTag("Target");
GameObject[] newTargets = new GameObject[targets.length - 1]; //sry for C#
for(var i = 0; i < newTargets.length; i++)
{
if(obj.GetInstanceID() != dyingObject.GetInstanceID())
{
newTargets[i] = obj;
}
}
}
you got my point perfectly.. Thanks for valuable suggestion. how can i do it in javascript? any idea?
I think i just need to remove object from the array when i'm destroying it from scene. right?
I hope that the following helps to clarify for you:
// Add the following to the top of your script:
import System.Collections.Generic;
// Create your list variable using this construct:
var newTargets:List.<GameObject> = new List.<GameObject>();
// Add to your list with:
newTargets.Add(obj);