- Home /
How do we add 2 GameObjects to an Array
Hi there, I am trying to add 2 Gameobjects to an GameObject array, but it says it :
Assets/WeaponScripts/SentryGun_plr.js(33,22): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.GameObject[]'
In the latter code it takes a GameObject and finds the closest one.
So, how to I add 2 Gameobjects to an GameObject array.
function FindClosestEnemy (): GameObject {
var moblist : GameObject[];
moblist = E1 && E2;
var closest: GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
for (var mobcheck : GameObject in moblist) {
var diff = (mobcheck.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = mobcheck;
distance = curDistance;
}
}
return closest;
}
E1 and E2 are FindWithTags.
Answer by Eric5h5 · Mar 07, 2012 at 10:16 AM
You can't use arrays like that, you have to define a size when you create the array. It also works if you assign content to the array when creating it.
var moblist = [E1, E2];
&& is used for boolean operations, you can't combine objects with it.
Your answer
Follow this Question
Related Questions
How can I make an NPC follow a path specified by an array? 0 Answers
Referencing arrays with a variable 1 Answer
Simple Looping drag and drop game 1 Answer
Efficient wind on clouds 2 Answers