Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Gnarfen · Jan 12, 2012 at 01:32 PM · variablecolorclassinheritanceprivate

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)


Comment
Add comment · Show 3
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 Kryptos · Jan 12, 2012 at 08:47 PM 0
Share

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?).

avatar image Larry-Dietz · Jan 12, 2012 at 09:53 PM 0
Share

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

avatar image Gnarfen · Jan 13, 2012 at 08:01 AM 0
Share

@$$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.

3 Replies

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

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);
Comment
Add comment · Show 6 · 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 Gnarfen · Jan 12, 2012 at 07:02 PM 0
Share

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.

avatar image ArcainOne · Jan 12, 2012 at 08:55 PM 0
Share

How about if you move it from Start to Awake?

avatar image ArcainOne · Jan 12, 2012 at 09:08 PM 0
Share

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.

avatar image syclamoth · Jan 12, 2012 at 09:40 PM 0
Share

If you make it public, your results could get corrupted by Unity's field serialization functionality.

avatar image ArcainOne · Jan 12, 2012 at 10:48 PM 0
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 1 · 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 Gnarfen · Jan 12, 2012 at 07:01 PM 0
Share

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;

avatar image
0

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

Comment
Add comment · Show 6 · 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 Gnarfen · Jan 12, 2012 at 08:20 PM 0
Share

Thanks for the suggestion! I've tried it now but it doesn't solve the problem tho :(

avatar image ArcainOne · Jan 12, 2012 at 09:21 PM 0
Share

Serialization is when you are sending that object over a network. Which I do not think is occurring here.

avatar image Kryptos · Jan 12, 2012 at 09:28 PM 0
Share

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.

avatar image ArcainOne · Jan 12, 2012 at 09:43 PM 0
Share

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".

avatar image Kryptos · Jan 12, 2012 at 10:20 PM 0
Share

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).

Show more comments

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

11 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

Related Questions

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Is it possible to set variables for classes? 2 Answers

How to save prefabs with private data through inheritance 1 Answer

How to build up the class structure for an object 1 Answer

Need Help With My GAME! 3 Answers


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