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;
}
}
}
I also get no errors stating that anything is wrong apart from this one:
You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.$$anonymous$$onoBehaviour:.ctor()
TheUI:.ctor()
$$anonymous$$aterialChanger:.ctor() (at Assets/$$anonymous$$aterialChanger.cs:8)
regarding this line of code:
TheUI myUI = new TheUI();
which is in the material changer script on line 8.
Answer by · Jul 27, 2017 at 10:24 PM
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
}
....
Answer by FortisVenaliter · Jul 27, 2017 at 10:16 PM
...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.
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
Yes, you do set it to not destroy on load. But... look at it this way:
You create the first TheUI properly, on a GameObject. Let's call this TheUI-A.
Then the next scene loads, and $$anonymous$$aterialChanger needs a reference to TheUI. But rather than finding TheUI-A, it says to create a new one (hence the 'new' keyword), which we'll call TheUI-B.
Now, TheUI-A has all the values, but no one is using them. But TheUI-B is being used by your script, but it's brand new and has none of it's values set.
Your answer
Follow this Question
Related Questions
C sharp get and set problem: "name doesn not exist in the current context 1 Answer
Modify Built-In Class Variables Get/Set functions 1 Answer
Getter and Setter Question! 1 Answer
Character Select Button Putting Info into GameManager Script - Is this Possible? 0 Answers
Get Set Return method, properties stuff? 2 Answers