- Home /
Finding multiple gameobjects with tag error
Ok i have multiple enemies and multiple players in my scene. My problem was that when i an enemy killed a player they would stop searching for players tagged "playerguy". So i tried changing GameObject.FindWithTag("PlayerGuy").Transform to GameObject.FindGameObjectsWithTag("PlayerGuy").Transform. Problem is i get this error
`Assets/TankMan/TankManScripts/EnemyScripts/Lvl1TerroristAI.js(29,74): BCE0019: 'transform' is not a member of 'UnityEngine.GameObject[]'.
I understand why im getting this error, I just dont know how to fix it. If anyone can point me in the direction of fixing the problem that would be great.
static var target1 : Transform;
var moveSpeed : int = 6; // chase speed
var rotationSpeed : int = 1; // speed to turn to the player
var maxDistance : int = 10; // attack distance
var minDistance : int = 15; // detection distance
private var myTransform : Transform;
function Awake() {
myTransform = transform;
}
// Cache the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);
function Start () {
target1 = GameObject.FindWithTag ("PlayerGuy").transform;
maxDistance = 2;
}
//------------------------------------------------------------------
function Update(){
if (target1 == null){
target1 = GameObject.FindGameObjectsWithTag("PlayerGuy").transform;
if (target1) {
var dist = Vector3.Distance(target1.position, transform.position);
if (dist > minDistance){ // if dist > minDistance: enters idle mode
Idle();
}
else
if (dist <= maxDistance) { // if dist <= maxDistance: stop and attack
print ("Attack!");
}
else { // if maxDistance < dist < minDistance: chase the player
// print ("I found you "+ dist);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position) , rotationSpeed * Time.deltaTime);
// Move towards target
characterController.Move(myTransform.forward * moveSpeed * Time.deltaTime);
//
//
}
if (dist <= maxDistance){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target1.position - myTransform.position) , rotationSpeed * Time.deltaTime);
}
}
}
}
//------------------------------------------------------------------
var walkSpeed = 3.0;
var directionTraveltime = 2.0;
var idleTime = 1.5;
var rndAngle = 45;
// enemy will turn +/- rndAngle
private var timeToNewDirection = 0.0;
private var turningTime = 0.0;
private var turn: float;
function Idle () {
// Walk around and pause in random directions unless the player is within range
if (Time.time > timeToNewDirection) {
// time to change direction?
if(Random.value > 0.5) // choose new direction
turn = rndAngle;
else { turn = -rndAngle; }
turningTime = Time.time + idleTime;
// will stop and turn during idleTime...
timeToNewDirection = turningTime + directionTraveltime;
// and travel during directionTraveltime
}
if (Time.time < turningTime) {
// rotate during idleTime...
transform.Rotate(0, turn*Time.deltaTime/idleTime, 0);
} else {
// and travel until timeToNewDirection
characterController.SimpleMove(transform.forward * walkSpeed);
}
}
function OnCollisionEnter(collision : Collision) {
transform.Rotate(0, turn*Time.deltaTime/idleTime, 0);
}
Please go back and reformat your code. To do it properly, Paste your entire code block in, highlight it, then hit the code (101010) button.
ah i see how to do it now. i was pressing 1010101 and then pasting the code
Answer by Airmand · Jul 31, 2011 at 02:33 AM
Got help from someone on the scripting forums and figured it out
function Update () {
var target1 : Transform;
var gOs = GameObject.FindGameObjectsWithTag("PlayerGuy");
if (gOs.Length > 0) {
var closestDistance : float=Mathf.Infinity;
for(var g : GameObject in gOs) {
var distance : float = Vector3.Distance (transform.position, g.transform.position);
if (distance < closestDistance) {
target1 = g.transform;
closestDistance = distance;
}
}
}
Answer by Peter G · Jul 29, 2011 at 04:44 PM
The problem is that you can't access the Transform of a collection of GameObjects like that. You have an array of GameObjects that you found with FindGameObjectsWithTag(). To access a specific one, you need a specific GameObject, now there are a number of ways to do this.
//The easiest
var gOs = GameObject.FindGameObjectsWithTag("PlayerGuy");
target1 = gOs[0].transform;
//Get the first element in the array.
or convert the entire array. If you only need one element, this might be a waste of memory.
//C#.
var gOs = GameObject.FindGameObjectsWithTag("");
var targets = Array.ConvertAll<GameObject , Transform>( gOs , x => x.transform );
ok with this updated script i get this error and now my enemies wont move at all.
UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function. Lvl1TerroristAI..ctor () (at Assets/Tank$$anonymous$$an/Tank$$anonymous$$anScripts/EnemyScripts/Lvl1TerroristAI.js:3)
var gOs = GameObject.FindGameObjectsWithTag("PlayerGuy");
var target1 = gOs[3].transform;
var moveSpeed : int = 6; // chase speed
var rotationSpeed : int = 1; // speed to turn to the player
var maxDistance : int = 10; // attack distance
var $$anonymous$$Distance : int = 1000; // detection distance
private var myTransform : Transform;
function Awake() {
myTransform = transform;
}
// Cache the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);
function Start () {
target1 = gOs[3].transform;
maxDistance = 2;
}
There not moving because you have an error and the script won't compile properly. $$anonymous$$ove the "var gOs = ..." into the first line of the Start()
function
when i move that into start() i get a whole new slew of errors. so i moved var target 1 in the start as well and then that gave me about 10 more errors as well
Alright, move gOs into the start function, and delete the assignment to target1
in the field initializer. Change this:
var target1 = gOs[3].transform;
into:
var target1;
Does gOs appear anywhere else in your code? If it does then you will need to make a few more $$anonymous$$or adjustments.
alright i dont get any more errors but my enemies still arent moving...
var target1; var moveSpeed : int = 6; // chase speed var rotationSpeed : int = 1; // speed to turn to the player var maxDistance : int = 10; // attack distance var $$anonymous$$Distance : int = 1000; // detection distance
private var myTransform : Transform;
function Awake() {
myTransform = transform;
}
// Cache the controller
private var characterController : CharacterController;
characterController = GetComponent(CharacterController);
function Start () {
var gOs = GameObject.FindGameObjectsWithTag("PlayerGuy");
maxDistance = 2;
Answer by Airmand · Jul 30, 2011 at 05:03 AM
hmm in runtime i get a null reference exception object reference not set to an instance of an object. and nothing shows up in the inspector to set...
well that's the problem, check if the NRE is because the gOs is null or if its because the target1 is null.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161955im_/https://answers.unity.com/themes/thub/images/avi.jpg)