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 /
avatar image
0
Question by Jaidan · Feb 23, 2015 at 04:41 PM · error messagegetcomponentgameobject.find

Errors with Script C# [GetComponent, FindGameObject]

I have some code that im using to simulate a player getting into one of my spaceships but it is not working, anyone know why. I am a beginner so i would not know the way to fix it unless i was told. Thank you


Code:


 using UnityEngine;
 using System.Collections;
 
 public class GetInOutShip : MonoBehaviour {
 
     public Camera mainCamera;
     public Camera shipCamera;
 
     public Component getInArea;
 
     public GameObject player;
     public GameObject ship;
 
     private ShipController shipControls;
     private FirstPersonController playerControls;
 
     bool inShip = false;
 
     void Start () {
 
         getInArea = GetComponentInChildren<BoxCollider>();
 
         player = GameObject.FindGameObjectWithTag("Player");
         ship = GameObject.FindGameObjectWithTag ("Ship");
 
         shipControls = GetComponent <ShipController>();
         playerControls = GetComponent <FirstPersonController>();
 
         shipCamera.enabled = false;
         mainCamera.enabled = true;
 
         shipControls.enabled = false;
         playerControls.enabled = true;
     
     }
 
     void OnTriggerEnter (Collider collision) {
 
             inShip = true;
             playerControls.enabled = !playerControls.enabled;
             shipControls.enabled = !shipControls.enabled;
             shipCamera.enabled = !shipCamera.enabled;
             mainCamera.enabled = !mainCamera.enabled;
     }
 
     void OnTriggerExit (Collider collision) {
             
             inShip = false;
     }
 }




Some errors im getting:


  • NullReferenceException: Object reference not set to an instance of an object GetInOutShip.OnTriggerEnter (UnityEngine.Collider collision) (at Assets/Scripts/GetInOutShip.cs:40)

  • NullReferenceException: Object reference not set to an instance of an object GetInOutShip.Start () (at Assets/Scripts/GetInOutShip.cs:33)

Comment
Add comment · Show 1
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 NoseKills · Feb 23, 2015 at 04:55 PM 0
Share

Seems like your playerControls variable is staying null. That means rhe GetComponent() at line 27 is not finding a script of that type on this gameObject you are calling it from. $$anonymous$$ake sure the gameobject that has the GetInOutShip component also has a FirstPersonController

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Feb 23, 2015 at 04:48 PM

Both those lines reference the playerControls so best guess is the player controls are on a different gameObject to the one this script is on. Most likely the player so try this:

 playerControls = player.GetComponent <FirstPersonController>();
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 Jaidan · Feb 23, 2015 at 05:07 PM 0
Share

Okay well i have got some more errors with my code, i need help sorting it out. I need it so that at the beginning the player and all its stuff is enabled and when it collides with the obect, the whole of the player (including camera) turns of, and Everything to do with the ship turns on


Code:


using UnityEngine; using System.Collections;

public class GetInOutShip : $$anonymous$$onoBehaviour {

 public Camera mainCamera;

 public Component getInArea;

 public GameObject player;
 public GameObject ship;

 private ShipController shipControls;
 private FirstPersonController playerControls;

 bool inShip = false;

 void Start () {

     getInArea = GetComponent<BoxCollider>();

     player = GameObject.FindGameObjectWithTag("Player");
     ship = GameObject.FindGameObjectWithTag ("Ship");
     shipControls = ship.GetComponent <ShipController>();
     playerControls = player.GetComponent <FirstPersonController>();

     mainCamera.enabled = true;

     shipControls.enabled = false;
     playerControls.enabled = true;
     player.SetActive (true);
 
 }

 void OnTriggerEnter (Collider collision) {

     Debug.Log("Hit!");

     inShip = true;
     playerControls.enabled = false;
     shipControls.enabled = true;
     mainCamera.enabled = false;
     player.SetActive(false);
     ship.SetActive(true);
 }

 void OnTriggerExit (Collider collision) {
         
     inShip = false;
 }

}

avatar image Mmmpies · Feb 23, 2015 at 05:18 PM 0
Share

And the new errors are?

avatar image Jaidan · Feb 23, 2015 at 07:13 PM 0
Share

Well the main camera starts not enabled even though i enable it at the start.

Therfore i cant see anything to begin with :/

avatar image ico0019 · Feb 23, 2015 at 07:21 PM 0
Share

try Camera.main.enabled = true; ins$$anonymous$$ds of mainCamera.enabled = true; PS: the main camera is automatically set up in unity, why do you wanna to set it again?

avatar image Jaidan · Feb 23, 2015 at 07:26 PM 0
Share

Yeah i think thats better, im going to mess around with it for a bit, and if i still get stuck then i will post again. Thank you so much for you responses so far. Thanks

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

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

Why does gameobject.Find/GetComponent not work when a new scene is loaded? 1 Answer

How do I access this value properly so my characters exp goes up? 1 Answer

GameObject.Find GetComponent C# 4 Answers

Best way to access variables of script from granchild of another object, into a different script? 1 Answer

How do I use GetComponent in one Line 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