Ok I’ve been fighting with this for hours and im lost.
camera is setup as point and click, camera follows the player, player moves using raytrace on mouse position.
i am trying to get the camera to change to a top down view vs a isometric view once the player enters a specific area.
so i put in a trigger.
attached the following script.
has a very basic script for testing, creates a public bool called topdown and sets it to false by default.
using UnityEngine;
using System.Collections;
public class SafeHouseTrigger : MonoBehaviour {
public bool topdown = false;
void OnTriggerEnter()
{
topdown = true;
}
// Use this for initialization
void Awake ()
{
}
void Start ()
{
//Debug.Log (ingame);
}
// Update is called once per frame
void Update () {
}
}
Very basic.
so in my camera script im just trying to test that the bool is working.
i have " public SafeHouseTrigger safehouseT;"
in void Awake () i have " safehouseT = GetComponent ();"
and in Update () i have " Debug.Log (safehouseT.topdown);"
and even though it auto completes as I’m typing and theirs is no errors anywhere.
as soon as i launch the game i get
NullReferenceException: Object reference not set to an instance of an object
GCamera.Update () (at Assets/Game Assets/GCamera/GCamera.cs:22)
I’m assuming both scripts are attached to the same GameObject with this response**
I’m not sure if the order of the instantiation of each Component of a GameObject is guaranteed if the ScriptExecutionOrder of these components are the same.
Extra information about Script Execution order, if its relevant
The important line from this documentation is this:
“By default, the Awake, OnEnable and Update functions of different scripts are called in the order the scripts are loaded (which is arbitrary)”
This leads me to believe that even if 2 Components reside on the same GameObject, if they have the same ScriptExecutionOrder, there’s no guarantee that one script will have its Awake called before the other despite their visual ordering in the Inspector of the Editor.
Maybe try using GetComponent() in the Start method of your Camera script as opposed to Awake, or consider modifying the ScriptExecutionOrder of your GCamera to be after whatever execution order your SafeHouseTrigger is in. I would only consider the latter approach if no other script has a dependency on the GCamera script’s Update [and some other methods] order of execution.
GetComponent only works if you’re trying to get a Component on the GameObject that the script is calling GetComponent on, or if you’re calling GetComponent directly from a GameObject instance. For example:
{
// GetComponent is valid within the GameObject this script belongs to
MyMonoBehaviour mmb = GetComponent<MyMonoBehaviour>();
}
{
// Attempting to get a Component that belongs to another GameObject
SomeComponent sc = someOtherGameObject.GetComponent<SomeComponent>();
}
Since your camera is just checking to see if your SafeHouseTrigger script has a value set, you can set the SafeHouseTrigger value through the Inspector by dragging the GameObject with the SafeHouseTrigger component onto the GCamera’s SafeHouseTrigger field before you run the application. Another option is to set a reference to the GameObject that has the SafeHouseTrigger component to a public GameObject variable in GCamera and then call GetComponent on that GameObject like the 2nd chunk of code suggests… but if the easier option satisfies your needs, why bother with more indirection?
wow, that was a stupid overlook on my part, thank you.
i was doing both for some reason, setting the component in the script as well as setting the component in the inspector.
removed it in the script and its working fine.
guess you overlook those things when you have been coding for 10 hours straight.