• 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 /
This question was closed Jul 03, 2015 at 11:28 AM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by lui222 · Jul 02, 2015 at 08:36 PM · errortagsmissing

Why?? MissingComponentException:

MissingComponentException:There is no 'GameObject' attached to the "Car" game object, but a script is trying to access it. You probably need to add a GameObject to the game object "Car"... Or your script needs to check if the component is attached before using it. Car.Update () (at Assets/Scripts/Car.cs:40)

Hello,

I have just started programming in Unity, and everything has been working fine, and for no reason, when I click, play in unity controller, the attached game object disconnects, and the text labels I have attached also change why is this?

Thank You, Marcell

Here is my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 
 public class Car : MonoBehaviour {
 
     public GameObject Car1;
     public GameObject FirstPersonController;
     public Camera CarCam;
     public Camera FirstPersonControllerCam;
     public Text CarGetIn;
     public Button CarGetInBut;
     public Text CarGetOut;
     public Button CarGetOutBut;
     int inCar=0;
     float dist;
 
     // Use this for initialization
     void Start () {
         Car1 = Car1.GetComponent<GameObject> ();
         FirstPersonController = FirstPersonController.GetComponent<GameObject> ();
         CarCam = CarCam.GetComponent<Camera> ();
         FirstPersonControllerCam = FirstPersonControllerCam.GetComponent<Camera> ();
         CarGetIn = CarGetIn.GetComponent<Text> ();
         CarGetInBut = CarGetInBut.GetComponent<Button> ();
         CarGetOut = CarGetIn.GetComponent<Text> ();
         CarGetOutBut = CarGetInBut.GetComponent<Button> ();
         CarGetIn.enabled = false;
         CarGetInBut.enabled = false;
         CarGetOut.enabled = false;
         CarGetOutBut.enabled = false;
 
 
     }
     void Update (){
 
     if (inCar == 0) {
             //dist = Vector3.Distance(Car1.transform.position, FirstPersonController.transform.position);
             if (dist < 5.00) {
                 CarGetIn.enabled = true;
                 CarGetInBut.enabled = true;
                 //print ("Test");
             } else {
                 CarGetIn.enabled = false;
                 CarGetInBut.enabled = false;
                 //    print ("Test");
             }
             //print (dist);
 
 
         } else {
             CarGetOut.enabled=true;
             CarGetOutBut.enabled=true;
         }
     }
     public void EnterCar(){
 
             inCar = 1;
         Car1.SetActive (true);
         CarCam.enabled = true;
         FirstPersonController.SetActive (false);
         FirstPersonControllerCam.enabled = false;
         CarGetIn.enabled = false;
         CarGetInBut.enabled = false;
 
 
     }
     public void LeaveCar() {
         inCar = 0;
         //Car1.SetActive (false);
         CarCam.enabled = false;
         FirstPersonController.SetActive (true);
         FirstPersonControllerCam.enabled = true;
         CarGetOut.enabled = false;
         CarGetOutBut.enabled = false;
 
     }
 }
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

  • Sort: 
avatar image
1

Answer by Bunny83 · Jul 02, 2015 at 08:53 PM

Those lines won't compile at all and don't make any sense:

 Car1 = Car1.GetComponent<GameObject> ();
 FirstPersonController = FirstPersonController.GetComponent<GameObject> ();

A GameObject is not a component and can't be passed as generic parameter to GetComponent as only types which are derived from Component are allowed.

Remove those two lines.

Further more those lines won't throw a compile error but make no sense either:

      CarCam = CarCam.GetComponent<Camera> ();
      FirstPersonControllerCam = FirstPersonControllerCam.GetComponent<Camera> ();
      CarGetIn = CarGetIn.GetComponent<Text> ();
      CarGetInBut = CarGetInBut.GetComponent<Button> ();

As well as those two which even seem to have a copy and paste error as you use "CarGetIn" and assign it to CarGetOut:

      CarGetOut = CarGetIn.GetComponent<Text> ();
      CarGetOutBut = CarGetInBut.GetComponent<Button> ();

All those GetComponent lines are total nonsense as you already assigned those references directly in the inspector.

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
avatar image
0

Answer by lui222 · Jul 03, 2015 at 06:57 AM

Hi, Thanks for your help, I figured it out by using transforms instead of gameobjects.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Debug.Log error 2 Answers

Freeze game using timer from start, then resume play when click the button. It is possible ? 0 Answers

Unity 3d WEB player dowload link is not working / broken,Unity 3d web player download link not working 0 Answers

Code error Javascript GUI text 0 Answers

The referenced script on this Behaviour is missing! non-existant GameObject 0 Answers


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