*Suddenly* GameObject does not contain a definition for 'FindGameObjectWithTag'
Hi,
First time posting question.
Yesterday after programing in unity, suddenly appeared in the console 73 errors.
Most of them was GameObject does not contain a definition for 'FindGameObjectWithTag' -
In addition there were these errors:
GameObject does not contain a definition for 'Find'. GameObject does not contain a definition for 'SetActive'.
Been working this game for month!. I have to get your help!!
Thanks.
can you share your code? can you try adding UnityEngine. in the findobjectrwithtag lines? like this
UnityEngine.GameObject.FindGameObjectWithTag("");
Answer by xxmariofer · Feb 25, 2019 at 10:27 AM
hello, if that solved some errors is because you have created a class called GameObject that is overrdiding unity implementation. if you didnt have any errors before i would sugest searching for that class (is as easy as remove the UnityEngine. that i suggested and right click and go to implementation there will be probably your custome class or a class from a plugin) or just search in the assets folder.
i will change it to an answer for future readers with the similar issues so you can change it to correct answer.
Answer by liormalichi · Feb 25, 2019 at 10:16 AM
HI,
Thank you for replying!
Adding "UnityEngine" solved some of the problem' it solved the part of FindGameObjectWithTag("") and Find().
still have errors related to GameObject such as:
FindGameObjectsWithTag (Objects in plural)
and SetActive()
in this code even if I add UnityEngine before FindGameObjectsWithTag (Objects in plural) it does not work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class obstacleMovement : MonoBehaviour
{
public Rigidbody2D rb;
public GameObject[] obstacle;
public Vector3 playerPosition;
public Vector3 Movement = new Vector3(0, 0, 0);
private Vector3 dir = new Vector3();
private float repelRange = 6f;
private Vector2 repelForce;
public Vector2 newPos;
public float speed;
public float maxDistanceFromPlayer;
void Start()
{
speed = UnityEngine.GameObject.Find("gamemanager").GetComponent<gameManager>().speed1;
maxDistanceFromPlayer = UnityEngine.GameObject.FindGameObjectWithTag("obstacle").GetComponent<ObstacleSpawning>().radius * 2.5f;
}
// Update is called once per frame
void FixedUpdate()
{
//Movement = Vector3.MoveTowards(transform.position, playerPosition, 10 * Time.deltaTime);
//Movement = new Vector3(Movement.x, Movement.y, 0f);
//rb.MovePosition(Movement);
obstacle = GameObject.FindGameObjectsWithTag("obstacle11");
playerPosition = UnityEngine.GameObject.FindGameObjectWithTag("Player").transform.position;
Vector2 repelForce = Vector2.zero;
foreach (GameObject obstacle1 in obstacle)
{
dir = rb.transform.position - obstacle1.transform.position;
Vector2 dir1 = dir;
if ((obstacle1.transform.position - rb.transform.position).magnitude <= repelRange)
{
repelForce += (rb.position - (Vector2)obstacle1.transform.position).normalized;
}
if ((playerPosition - obstacle1.transform.position).magnitude >= maxDistanceFromPlayer)
{
Destroy(obstacle1);
}
}
Vector2 dirr = ((Vector2)playerPosition - (Vector2)rb.position).normalized;
float rotateAmount = Vector3.Cross(dirr, transform.up).z;
float turnSpeed = 200f;
Vector2 newPos = transform.position + transform.up * Time.fixedDeltaTime * speed;
newPos += repelForce * Time.deltaTime;
rb.MovePosition(newPos);
rb.angularVelocity =-rotateAmount * turnSpeed;
//following script lines make the game harder
//they make enemies faster but spawn them farther away
}
}