- Home /
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)
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
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>();
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;
}
}
Well the main camera starts not enabled even though i enable it at the start.
Therfore i cant see anything to begin with :/
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?
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
Follow this Question
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