- Home /
C# Cannot reference scripts on the FirstPersonCharacter
Well, I have a few image affecting scripts such as antialiasing, bloom, sunshafts, colorcorrection etc.. And in an options section of my pause menu, I want to add the option of either disabling them or enabling them. Specifically right now I am working with the Sun Shafts. I set it up to that when I click a button on the UI, it calls the function "selectSunShafts()" which disables the SunShafts script on the FirstPersonCharacter under unity's standard FPS Controller. Sadly when I play test in Unity, I get the error "NullReferenceException". I have tried to disable this many ways and none are working. Any help would be greatly appreciated.
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine.UI;
public class Options : MonoBehaviour {
public Text sunShafts;
void Awake()
{
sunShafts.text = "On";
}
public void selectSunShafts()
{
GameObject fpc = GameObject.Find("FirstPersonCharacter");
fpc.GetComponent<SunShafts>().enabled = false;
sunShafts.text = "Off";
}
}
You haven't told us which line throws the error.
If it's line 14 then either you don't have an active GameObject called "FirstPersonCharacter", or the one it finds doesn't have a SunShafts component. In other words either fps is null or fps.GetComponent() is null. I would add logging after the call to Find to find out which it is, and take it from there.
@Bonfire-Boy @Darkwinger @villevli Thanks for the helpful suggestions :) I will be trying them tomorrow and I'll let you know how it goes.
@Bonfire-Boy @Darkwinger @villevli Yeah, unfortunately none of the above worked. I did double check and yes, there is a sun shafts script on my first person character, but regardless, the script cannot seem to find it..!
Use Comments to post replies to people, the "Your Answer" thing is for answers to the original question. I've converted the above to a comment.
With regard to my earlier comment, have you worked out whether it's not finding the GameObject, or finding the GameObject but not a Sunshafts component?
Answer by Darkwinger · Aug 31, 2016 at 05:28 PM
Use
public Sunshafts script;//Sunshafts is the name of the mono behaviour class
Sunshafts.enabled=false;
You might want to use ! to set it to the opposite of what it is, to simplify the code.
But Sunshafts is a class not an instance. I wouldn't expect the above to even compile.
If the sunshafts are in the scene from the start then yes, one could hook them up to a public reference in the inspector and get around the need for the Find call. But that's not always possible.
@Bonfire-Boy I use it to get variables from other scripts, and it can be used to stop all update etc. from running, and enabled again.
I'm sorry, I don't understand how that relates to my comment.
Answer by villevli · Aug 31, 2016 at 07:19 PM
In the FPSController prefab from Standard Assets, the "FirstPersonCharacter" object is a child object of the "FPSController" object, so you would have to call: GameObject.Find("FPSController/FirstPersonCharacter"); to find it. That may be your problem here.
Your answer
Follow this Question
Related Questions
Error CS0029: Cannot implicitly convert type to UnityEngine.UI.Transform 1 Answer
My Script Wont work? 2 Answers
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers
IL2CPP does not support marshaling delegates that point to instance methods to native code. 1 Answer