Hey there, hop you’re all fine.
Kind of new here !
Sorry for the long post
So my problem is that I got an ennemy tank with some really simple scripts, one for an IDLE moving / patrolling, one that hold a bool “AlertState” and that will in the future make things when the player is near, and one “GameController” that should handle the functions and call the ones at the great moments (What allow me to just put functions in the scripts, and the GameController just contain the behavior of the Ennemy).
But I got this error :
MissingComponentException: There is no ‘GameObject’ attached to the “Ennemy” game object, but a script is trying to access it.
You probably need to add a GameObject to the game object “Ennemy”. Or your script needs to check if the component is attached before using it.
Here is my GameController script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ennemy
{
public class Ennemy_GameController : MonoBehaviour {
private Ennemy_IDLE ennemy_IDLE;
private Ennemy_Detect ennemy_Detect;
public GameObject me;
void Awake ()
{
me = GetComponent<GameObject> ();
ennemy_IDLE = me.GetComponent<Ennemy_IDLE> ();
ennemy_Detect = me.GetComponent<Ennemy_Detect> ();
}
void Update ()
{
if (ennemy_Detect.alertState == true) {
ennemy_IDLE.isIDLE = false;
ennemy_IDLE.inMove = false;
} else {
ennemy_IDLE.isIDLE = true;
}
}
}
}
Here is my script to detect the ennemy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ennemy
{
public class Ennemy_Detect : MonoBehaviour {
public bool alertState;
void Awake ()
{
alertState = false;
}
void OnTriggerStay2D (Collider2D other)
{
if (other.CompareTag ("Player")) {
alertState = true;
}
}
}
}
And then here is my script to move the ennemy when IDLE (longer, but a lot is just on moving the ennemy)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ennemy
{
public class Ennemy_IDLE : MonoBehaviour {
public int speed;
public bool isIDLE;
public bool inMove;
private bool cooldown;
private float timerMovement;
private float timerIntermediaire;
private int direction;
void Awake ()
{
isIDLE = true;
inMove = false;
cooldown = false;
}
void FixedUpdate ()
{
IDLETest ();
IDLEMoving ();
}
void IDLETest ()
{
if (isIDLE == true && inMove == false && cooldown == false) {
direction = Random.Range (0, 3);
timerMovement = Random.Range (2, 3);
timerIntermediaire = Random.Range (2, 3);
inMove = true;
}
}
void IDLEMoving ()
{
if (isIDLE == true && inMove == true && cooldown == false) {
if (timerMovement > 0) {
switch (direction) {
case 0:
timerMovement -= Time.fixedDeltaTime;
transform.Translate (Vector3.up * speed * Time.deltaTime);
break;
case 1:
timerMovement -= Time.fixedDeltaTime;
transform.Translate (-Vector3.right * speed * Time.deltaTime);
break;
case 2:
timerMovement -= Time.fixedDeltaTime;
transform.Translate (-Vector3.up * speed * Time.deltaTime);
break;
case 3:
timerMovement -= Time.fixedDeltaTime;
transform.Translate (Vector3.right * speed * Time.deltaTime);
break;
default:
Debug.Log ("Erreur direction dans Ennemy_IDLE");
break;
}
} else {
cooldown = true;
}
}
else if (cooldown == true) {
inMove = false;
if (timerIntermediaire > 0) {
timerIntermediaire -= Time.fixedDeltaTime;
} else {
cooldown = false;
}
}
}
}
}
The GameController and the Mover when IDLE are attached the ennemy itself, but the script that I use to detect the player is attached to a child GameObject that hold a circle collider.