- Home /
There is no GameObject attached to this GameObject
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.
Look in the editor in the assignable values, you're missing a component there somewhere.
No, in the editor in "$$anonymous$$e" I've assigned the "Ennemy" gameobject
I've also tried to put this private and not to assign it in the inspector but in the script but got the same Error.
Answer by unidad2pete · Aug 14, 2017 at 05:29 PM
me = GetComponent <GameObject>();
This is your problem, GameObject its not a component.
You cant use " this " , that is a reference to self-reference to Script, not to gameObject.
For your own gameObject reference is :
me = gameObject;
Change that code and should be fixed.
And you dont need me reference, you can get component directly:
void Awake ()
{
ennemy_IDLE = GetComponent<Ennemy_IDLE> ();
ennemy_Detect = GetComponent<Ennemy_Detect> ();
}
Ho...did not make it that way... I just deleted the "me" variable and replaced it by "this" before the getcomponent for the ennemy_IDLE and ennemy_Detect
is one of these two ways better than the other ?
No , Does not matter :) Its just the same thing, maybe to 100% optimization, its better without "this", because its not necessary, but there are not problem.
Answer by Destolos · Aug 14, 2017 at 05:16 PM
The problem is, probably, that you use "me = GetComponent ();". You don't need to do this, instead you could write directly "ennemy_IDLE = GetComponent ();". You also can save this two lines, when you make "ennemy_IDLE" and "ennemy_Detect" public and use the inspector to assign them.
it works ! Did not think about this keyword..
$$anonymous$$any thanks for your help ! :)
Yea, but I try not to use the insector because I wish to use this as a prefab later and so I need to be able to spawn it without manual modifications in the inspector :)
But I tried to add the "me" (That was wrong here) because without it, it was "not set to an instance of an object".
$$anonymous$$any thanks for your help :)