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*.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();
    }
  • }*

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();
        }
    }