Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
0
Question by goboy3133 · Jun 21, 2017 at 03:54 PM · scripting problemgameobjectcomponentenable and disable script

"There is no 'GameObject' attached to the "x" game object'

I'm trying to have a script change two objects in my game between enabled and disabled, and I have a script to get the component of both of these game objects, but every time I run my game, the game objects seem to disappear from the 'GetComponent' section of the script, thus giving me the error that 'There is no 'GameObject' attacked to the "x" game object'. I don't know why this keeps happened, or what to do to fix it, so I was hoping that someone could point me in the right direction?

This is my script that changes the stats of the game objects:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Door : MonoBehaviour
 {
     public GameObject doorOpen;
     public GameObject doorClosed;
     public bool Collide;
     public bool doorShut;
 
     // Use this for initialization
     void Start()
     {
         doorOpen = GetComponent<GameObject>();
         doorClosed = GetComponent<GameObject>();
         doorShut = true;
         doorClosed.SetActive(true);
         doorOpen.SetActive(false);
 
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     void OnGUI()
     {
         if (Collide == true)
         {
             if (doorShut)
             {
                 GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 10, 200, 100), "Press E to Open");
             }
         }
     }
     void OnTriggerEnter(Collider other)
     {
 
         if (other.tag == "Player")
         {
             Collide = true;
             if (Input.GetKeyDown(KeyCode.E))
             {
                 doorClosed.SetActive(false);
                 doorOpen.SetActive(true);
                 doorShut = false;
             }
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             Collide = false;
         }
     }
 }

Here are pictures of the script before running and after running:

Not Running: Not Running

Running: Running

Thank you!

nopt-playing.png (20.2 kB)
playing.png (20.1 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by cstooch · Jun 21, 2017 at 04:17 PM

Try deleting the first two lines in your start.. you already do this in inspector, so you shouldn't need to look for any components.

Comment
Add comment · Show 5 · 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 cstooch · Jun 21, 2017 at 04:23 PM 0
Share

Sorry ignore this follow up post.. I get what you're doing. I think just removing the lines i suggested in start should do it.

avatar image goboy3133 · Jun 21, 2017 at 10:01 PM 0
Share

I tried this this morning (with a movement of the if(input), and it seems to be all working now!

Thank you!!

avatar image cstooch goboy3133 · Jun 21, 2017 at 10:35 PM 0
Share

No problem, glad it worked!

avatar image YaoZhang · Mar 29, 2019 at 08:39 AM 0
Share

Nice, exactly the key to solve the problem!

avatar image Lexudau · Dec 04, 2019 at 11:24 PM 0
Share

Thank you so much. This is eyeopening.

avatar image
1

Answer by tanoshimi · Jun 21, 2017 at 10:06 PM

A gameobject is not a component, so the syntax GetComponent<GameObject>() doesn't make any sense. Components are attached to gameobjects. Since you already have a reference to the doorOpen and doorClosed gameobjects you should just call .SetActive directly on them.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

121 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

Related Questions

Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer

How to add components to other GameObjects through a C# script attached to one? 2 Answers

Can't disable component, little checkbox is missing from inspector 1 Answer

How to handle coupled components in Unity 2 Answers

Access water4 scripts via my script? 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