Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 fergie77 · Aug 21, 2017 at 03:12 PM · scripting problemprefabreferenceonclickprefab connection

Object reference not set to an instance of an object (Scripts calling objects which only exist in certain scenes)

Right, I feel like despite the title of this question, it's a bit more in depth.

Basically I have a pause menu which references various objects in the scene. It contains buttons which allow the player to change colour, and abilities given to that colour. So I have it set up that each button references a "colour change" script onClick which has different methods to set each colour mode (red mode(), blueMode() etc).

This colour change script also references the script which controls the pause menu. So when you select a colour, it calls the pause menu script and the "Resume()" method.

My problem is that I seem to need to attach all of the references manually to each object in the scene containing the scripts through the editor. As using a "GameObject.find.." will flash up an "object reference not set to..." in scenes not containing those objects (main menu, level select menu).

I feel like it would be easier if I could just reference the other scripts to call these functions, rather than the objects in the scenes which the scripts are attached to?

No doubt I'm doing something completely stupid but right now I have no clue! Also apologies if this has been answered before, I didn't know what to search!

Also for the colour change buttons, the pause menu will be in every level. Currently I have to assign each button the reference to the colour change script and select the method for onClick in the editor. They are all prefabs and yet when I click apply, this setting doesn't persist between scenes?

Here's my colour change script.

  public Color32 playerColour;
     public SpriteRenderer[] sprites;
     public pauseMenu pauseM;
     public bool blueBool = false;
     public bool redBool = false;
     public bool greenBool = false;
     public bool yellowBool = false;
 
     // Use this for initialization
     void Start () {
         normalMode();
     }
     
     // Update is called once per frame
     void Update () {
         for (int i = 0; i < sprites.Length; i++)
         {
             sprites[i].color = playerColour;
         }
     }
 
     public void normalMode()
     {
         playerColour = new Color32(191, 191, 191, 255);
         pauseM.Resume();
         blueBool = false;
         redBool = false;
         greenBool = false;
         yellowBool = false;
     }
     public void blueMode(){
         if (!blueBool)
         {
             playerColour = Color.blue;
             pauseM.Resume();
             blueBool = true;
         }
         else
         {
             normalMode();
         }
     }
     public void redMode()
     {
         if (!redBool)
         {
             playerColour = Color.red;
             pauseM.Resume();
             redBool = true;
         }
         else
         {
             normalMode();
         }
     }
     public void greenMode()
     {
         if (!greenBool)
         {
             playerColour = Color.green;
             pauseM.Resume();
             greenBool = true;
         }
         else
         {
             normalMode();        
         }
     }
     public void yellowMode()
     {
         if (!yellowBool)
         {
             playerColour = Color.yellow;
             pauseM.Resume();
             yellowBool = true;
         }
         else
         {
             normalMode();
         }
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by unidad2pete · Aug 21, 2017 at 03:50 PM

If the script with the function you need is named "ScriptWhitFunctions" for example, you can find all those scripts in the scene:

 public ScriptWhitFunctions[] scriptsToFound;
     
     void Start()
     {
         scriptsToFound = FindObjectsOfType<ScriptWhitFunctions>();
         foreach (ScriptWhitFunctions script in scriptsToFound)
         {
             script.FunctionToCall();
         }
     }
Comment
Add comment · Show 2 · 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 fergie77 · Aug 21, 2017 at 04:00 PM 0
Share

Hi, I'm not quite sure what you mean. I'm not really looking for scripts. The problem I have is that the scripts reference each other but have to be assigned in the editor. This link is lost between scenes?

avatar image unidad2pete fergie77 · Aug 21, 2017 at 04:24 PM 0
Share

From what I understand, you have a button that when you click it, changes something in the scripts you have previously assigned in the editor, but when changing the scene, those scripts may not exist in the current scene and when you click, you receive null exception, It is right?

With this code, assigned to the OnClick of the button, searches for all the objects that contain the component that you want to access and you can change its properties or call its functions, if there are none in the scene nothing happens, because in each click on button, searchs for objects that currently exist.

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

126 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 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 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 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 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 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

How to access a script on a instantiated prefab 1 Answer

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

AddListener() Javascript 2 Answers

The associated script cannot be loaded using UI prefab 0 Answers

How to set prefab's script value programatically? 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