- Home /
Assets/Scripts/Enemy.cs(61,44): error CS1955: The member `UnityEngine.GameObject.tag' cannot be used as method or delegate
i'm just stuck... here's my code
enter code hereusing UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
static bool go_left = false;
private int timer = 300;
private int rand_num;
static int num_chosen;
public GameObject Enemy_bullet;
public float speed;
void Start()
{
}
// Update is called once per frame
void Update ()
{
if(go_left)
transform.position = transform.position + new Vector3 (-.25f, 0, 0);
else
transform.position = transform.position + new Vector3 (.25f, 0, 0);
timer -=1;
if (timer <2)
{
rand_num = Random.Range(0, 20);
num_chosen = rand_num;
if (num_chosen == 2)
{
Instantiate(Enemy_bullet, transform.position, transform.rotation);
Vector3 newPosition = transform.position;
newPosition.y -= speed * Time.deltaTime;
transform.position = newPosition;
}
timer = 300;
}
}
void OnTriggerEnter(Collider col)
{
if(col.gameObject.name == "Left_Wall")
{
go_left = false;
MoveDown();
}
else if (col.gameObject.name == "Right_Wall")
{
go_left = true;
MoveDown();
}
}
void MoveDown()
{
string[] enemies = new string[1] {"aliens"};
string aliens = GameObject.FindGameObjectsWithTag("Enemy");
foreach (var entity in enemies)
transform.position = transform.position + new Vector3 (0, -.5f, 0);
}
}
Answer by robertbu · Apr 17, 2014 at 10:21 PM
GameObject.FindGameObjectsWithTag() returns an array of GameObjects. So the line should read:
GameObject[] aliens = GameObject.FindGameObjectsWithTag("Enemy");
'aliens' is local, and you are not currently using it, so I'm not sure of the intent of this particular line of code.
P.S. The error you list is not what I would expect from this code, though the line is clearly wrong. Are you sure this exact code generated that error?
changed up the coding to this
void $$anonymous$$oveDown()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (var entity in enemies)
transform.position = entity.transform.position + new Vector3 (0, -.5f, 0);
}
so now it kind of works but it buched them together. this is for a space invaders project btw. the section of code is to get all the aliens to move down at once.
If that is the goal (moving the enemies) then your final line does not seem to be right to me. Try this ins$$anonymous$$d:
entity.transform.position = entity.transform.position + new Vector3 (0, -.5f, 0);
If you question is answered, please click on the checkmark next to the answer to close it out. Thanks.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I convert Component[] array into String[] with GetComponentsInChildren? 1 Answer
Multiple Choice Game of Questions stored in an array 1 Answer
Sequence Scripting 2 Answers
Get multiple variables through a string. 2 Answers