Destroy on load/Properties Issue

So I have been trying and trying to resolve this issue. Basically the integer value for the color is assigned to theUI script in one scene and It is defiantly assigned. Next scene the Material changer script says that the color value is 0. Help me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TheUI : MonoBehaviour {

private int color;
public Button multiplayer;
public Text multiplayertext;
public Button exit;
public Text exittext;
public Text ColorText;
public Dropdown ColorBox;
public Text ColorBoxText;
public Image ColorBoxArrow;

public int ColorProperty
{
    get
    {
        return color;
    }
}

private void Awake()
{
    DontDestroyOnLoad(transform.gameObject);
}

private void Start()
{
    ColorText.enabled = false;
    ColorBox.enabled = false;
    ColorBoxText.enabled = false;
    ColorBoxArrow.enabled = false;
}

public void ColorSelect()
{
    multiplayer.enabled = false;
    exit.enabled = false;
    multiplayertext.enabled = false;
    exittext.enabled = false;
    ColorText.enabled = true;
    ColorBox.enabled = true;
    ColorBoxText.enabled = true;
    ColorBoxArrow.enabled = true;
}

public void Color()
{
    color = ColorBox.value;
    Debug.Log("the color is" + color);
}

public void Exit()
{
    Application.Quit();
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaterialChanger : MonoBehaviour {

    public int color;
    TheUI myUI = new TheUI();

    void Update () {
        color = myUI.ColorProperty;
        Debug.Log(color);
        if (color == 4)
        {
            gameObject.GetComponent<Renderer>().material.color = Color.blue;
        }
        if (color == 3)
        {
            gameObject.GetComponent<Renderer>().material.color = Color.red;
        }
        if (color == 2)
        {
            gameObject.GetComponent<Renderer>().material.color = Color.yellow;
        }
        if (color == 1)
        {
            gameObject.GetComponent<Renderer>().material.color = Color.green;
        }
    }
}

…Yeah, of course it does. You don’t set the reference to the instance of the first object, you set it to ‘new TheUI()’ which would be left completely blank since it wouldn’t even be attached to a GameObject.

You need to set the reference properly instead.

You should not instantiate the the class TheUI. Instead you need to find the reference for the already created one in the scene.

public class MaterialChanger : MonoBehaviour {
 
     public int color;
     TheUI myUI;

void Start()
{
        myUI = Object.FindObjectOfType<TheUI>(); //You can also find the gameObject and then getting it's component
}

 ....

I also get no errors stating that anything is wrong apart from this one:

You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
TheUI:.ctor()
MaterialChanger:.ctor() (at Assets/MaterialChanger.cs:8)

regarding this line of code:

TheUI myUI = new TheUI();

which is in the material changer script on line 8.

either im a bit dumb and i dont know what you said or dosen’t TheUI stay on the object its on as i used dontdestroyonload which stops the object from being destroyed