• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Nucl3ear · Aug 14, 2017 at 07:45 AM · c#scripting problemgameobjectinspectoreditor-scripting

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.

Comment
AmazinJacks

People who like this

1 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Aug 14, 2017 at 08:14 AM 0
Share

Look in the editor in the assignable values, you're missing a component there somewhere.

avatar image Nucl3ear · Aug 14, 2017 at 01:44 PM 0
Share

No, in the editor in "Me" I've assigned the "Ennemy" gameobject

avatar image Nucl3ear · Aug 14, 2017 at 05:05 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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> ();
          }



Comment
Nucl3ear
luciano_cardoso

People who like this

2 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nucl3ear · Aug 14, 2017 at 05:35 PM 0
Share

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 ?

avatar image unidad2pete Nucl3ear · Aug 14, 2017 at 05:40 PM 0
Share

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.

avatar image Nucl3ear unidad2pete · Aug 14, 2017 at 05:41 PM 0
Share

All right ! Many thanks ! :)

avatar image

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.

Comment
Nucl3ear
luciano_cardoso

People who like this

2 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nucl3ear · Aug 14, 2017 at 05:32 PM 0
Share

it works ! Did not think about this keyword..

Many thanks for your help ! :)

avatar image Nucl3ear Nucl3ear · Aug 14, 2017 at 05:44 PM 0
Share

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".

Many thanks for your help :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

395 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Arrays/Lists in the inspector of Editor Extension scripts 0 Answers

Reorderable list of UnityEvents 2 Answers

Update list on mouse click 1 Answer

Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer

Detect if build target is installed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges