Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 25, 2015 at 05:27 PM by fafase for the following reason:

Question is of type NullReferenceException

avatar image
0
Question by Jaidan · Feb 25, 2015 at 05:01 PM · object-reference-errornull reference

Object reference not set to an instance of an object? C#

 using UnityEngine;
 using System.Collections;
 
 public class GetInOutShip : MonoBehaviour {
 
     //Variables
     bool inShip = false;
     public Camera camera;
 
     public ShipController shipController;
     public FirstPersonController firstPersonController;
 
     public GameObject ship;
     public GameObject player;
 
     public Transform cameraTransform;
 
     void Start () {
 
         inShip = false;
         player.SetActive (true);
         ship.SetActive (true);
 
         GameObject.Find ("Spacship_vehicle").GetComponent<ShipController> ().enabled = false;
         GameObject.Find ("Player_player").GetComponent<FirstPersonController> ().enabled = true;
 
         cameraTransform  = Camera.main.transform;
     }
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.P)) {
             
             Invoke ("IntoShip", 0);
         }
     }
 
     void IntoShip() {
 
         Debug.Log ("Entered the Ship");
         GetComponent<FirstPersonController> ().enabled = false;
         GetComponent<ShipController> ().enabled = true;
         inShip = true;
         cameraTransform.parent = transform;
     }
 
     void ExitShip () {
 
         Debug.Log ("Left the Ship");
     }
 }


The error is on line 41. Someone please help i dont have a clue?

Comment
Add comment · Show 4
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 Bonfire-Boy · Feb 25, 2015 at 05:12 PM 0
Share

Do you have a FirstPersonController and a ShipController component on this GameObject?

avatar image fafase · Feb 25, 2015 at 05:12 PM 0
Share

Your component is on a different object since you use the same line in the start by finding first the object holding that script.

avatar image Bonfire-Boy · Feb 25, 2015 at 05:20 PM 0
Share

Ah, I think I see the thing, in Start() you look for particular objects then set their components enabled or not.

In IntoShip() you don't look for other objects, so it's looking for those components on the current object, but it doesn't have them (they're on those other objects) hence the error.

I think that making the lines in IntoShip() look like those in Start() would probably fix the error. But the Find (and to a lesser extent GetComponent) calls are expensive, so you'd be better off storing these components in your firstPersonController and shipController variables (probably making them private too), and then you'll just be able to do firstPersonController.enabled = true and so on

avatar image Jaidan · Feb 25, 2015 at 08:11 PM 0
Share

Hello, sorry i was out, i do not have both scripts on the gameobject so how would i reference to it in this script?

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by meat5000 · Feb 25, 2015 at 08:17 PM

You are not actually doing anything with your gameobject.Find calls. You make the calls but you dont store the result!

Store the gameobject references and use getcomponent from them to access the scripts kept on them.

 someGameObject.GetComponent<Script>().variable
Comment
Add comment · Show 1 · 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 Jaidan · Feb 25, 2015 at 08:35 PM 0
Share

Thank you.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

help I know I'm over looking something 0 Answers

Object not set to a reference(again) 1 Answer

Network Respawn Object not set... 0 Answers

NullReferenceException Error 1 Answer

Object reference not set to an instance of an object... again 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