Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aleksandrcooper · Aug 31, 2016 at 05:34 AM · c#scripting problemerror

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";
     }
 }

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 · Aug 31, 2016 at 11:40 AM 0
Share

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.

avatar image aleksandrcooper · Sep 02, 2016 at 09:16 AM 0
Share

@Bonfire-Boy @Darkwinger @villevli Thanks for the helpful suggestions :) I will be trying them tomorrow and I'll let you know how it goes.

avatar image aleksandrcooper · Sep 03, 2016 at 03:28 AM 0
Share

@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..!alt text

capture.png (40.5 kB)
us2.png (63.8 kB)
avatar image Bonfire-Boy aleksandrcooper · Sep 04, 2016 at 03:53 PM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

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.

Comment
Add comment · Show 3 · 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 Bonfire-Boy · Aug 31, 2016 at 11:22 PM 0
Share

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.

avatar image Darkwinger · Sep 01, 2016 at 12:36 PM 0
Share

@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.

avatar image Bonfire-Boy Darkwinger · Sep 01, 2016 at 01:07 PM 0
Share

I'm sorry, I don't understand how that relates to my comment.

avatar image
0

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.

Comment
Add comment · 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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hey guys, im starting do make a RPG game and in the Level system script(I called it "PlayerStatsController") i have an error: Parser Error : Unexpected symbol `public'. My script above: 1 Answer

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


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