- Home /
Private variable is not saved
Hi! I have a base class which all units inherits from which works fine. In this class I have a function to change the color of the gameObject when it is selected/deselected. However, the private variable which stores the original color only uses the color I define when declaring it. The variable does not update to the actual original color of the current gameObject. Here is my code:
using UnityEngine;
using System.Collections;
public class UnitBaseClass : MonoBehaviour {
private bool isSelected = false;
private Color originalColor = Color.cyan;
// Use this for initialization
void Start () {
// Set color to all possible child objects
MeshRenderer[] meshRenderer = this.GetComponentsInChildren<MeshRenderer>();
originalColor = meshRenderer[0].material.color;
Debug.Log("OriginalColor: " + originalColor);
foreach (MeshRenderer mesh in meshRenderer) {
mesh.material.color = originalColor;
}
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
// Toggle selected flag
if (isSelected==false) {
isSelected = true;
Debug.Log(gameObject + " selected");
// Mark selection with color by looping through all the children objects within the hitbox
MeshRenderer[] meshRenderer = this.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer mesh in meshRenderer) {
mesh.material.color = Color.red;
}
}
else {
isSelected = false;
Debug.Log(gameObject + " deselected");
// Mark selection with (original) color
MeshRenderer[] meshRenderer = this.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer mesh in meshRenderer) {
mesh.material.color = originalColor;
}
Debug.Log("OriginalColor: " + originalColor);
}
}
}
Below is my debug log which shows that the color is set the first time it is run (the Start-function), but after the select/deselect the original color changes back to cyan (which I used when declaring it). Why is the color not saved in the private variable? Any help is appreciated!
OriginalColor: RGBA(1.000, 1.000, 1.000, 0.500)
ship_hull (UnityEngine.GameObject) selected
ship_hull (UnityEngine.GameObject) deselected
OriginalColor: RGBA(0.000, 1.000, 1.000, 1.000)
How many objects do you have in your scene ? Did you try with only one ?
Because On$$anonymous$$ouseDown() will be called on every object regardless if it is selected or not. So maybe your are confused with the log, but every thing is still fine (did you check whether "collapse" is unchecked in the debug console?).
I just created a blank project in Unity, and trew a cube in the scene, and attached your code to it as is, and it worked perfectly fine. I couldnt duplicate the problem.
Do you have any code in other methods that may be altering the value? like a $$anonymous$$ouseUp event or something?
-Larry
@$$anonymous$$ryptos I have several objects in the scene which inherits from this base class. The log is not collapsed, it should show everything.
@Larry Dietz Very strange that you got it working but not me... The classes/objects which inherits from this class does not use (yet) any mouse related event-function.
Answer by ArcainOne · Jan 12, 2012 at 04:15 PM
Try forcing it to keep your stored color, it may be that you are keeping a reference to what is stored in meshRenderer material color instead of the actual Color Object.
In your Start function
Color meshColor = meshRenderer[0].material.color;
originalColor = new Color(meshColor.r, meshColor.g, meshColor.b, meshColor.a);
Thanks for your answer. I tried your suggestion but got the same result :( It still reads from the declaration color value ins$$anonymous$$d of the new value from the Start-function.
Actually... What you may want to do is simply make that a public property for debugging purposes. That way you can see exactly how that property is changing during run time. I find this helps when something like that occurs to me.
If you make it public, your results could get corrupted by Unity's field serialization functionality.
It may... but I've never had it happen. Besides it is only for debugging purposes. If you manage to spot your problem its worth it. In the end you make it private again anyway. You'll at least be able to identify if it is changing and what events cause it to change if any.
Answer by bodec · Jan 12, 2012 at 03:58 PM
I am still a lottle new to coding but im not seeing in the code where you reasign originalColor to something differnt. I only see where you made you first var at the top
I'm setting the original color in my Start-function with these lines:
// Set color to all possible child objects $$anonymous$$eshRenderer[] meshRenderer = this.GetComponentsInChildren<$$anonymous$$eshRenderer>(); originalColor = meshRenderer[0].material.color;
Answer by jrobichaudg · Jan 12, 2012 at 08:06 PM
Have you tried to add "[SerializeField]" attribute to your variables? http://unity3d.com/support/documentation/ScriptReference/SerializeField.html
Thanks for the suggestion! I've tried it now but it doesn't solve the problem tho :(
Serialization is when you are sending that object over a network. Which I do not think is occurring here.
No it is not. Serialization is when data is saved to disk and/or sent over a network. Your game object's properties are saved thanks to serialization.
That is correct, I do tend to forget that... for some reason. But still I do not think it would help in this instance as the object is not moving between a network or disk or other "medium".
Yes it is moved to disk (or at least to virtual memory) since it is saved by Unity (either as a single instance in a scene or as a prefab).