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 aga · Mar 05, 2015 at 12:39 AM · c#inspectorclass

Can't edit referenced class in the inspector

Hi everyone,

I've come across a weird issue. I hope I can make it clear with the following example:

Ok, I'm working on a turn based game, where I want to keep all players in a List<>, which can be viewed and edited in the inspector. I also want the current player to be referenced in a variable called "current".

Now, the weird thing is that as soon as I reference a Player from the List<> into "current",

 current = players[index];

I can no longer edit that Player from the List<> displayed in the inspector. However, all other Players in the list CAN be edited. Why is that?

Please note that editing the player using its reference (current) updates it's values in the list as expected. Editing values inside the script itself also works without a problem.

 current.health= 10;
 players[index].health = 20;

Obviously this isn't much of a problem, but I am curious as to what I'm missing. Thanks for taking the time!

Code I'm using.

 [System.Serializable]
 public class Player
 {
     public float health;
 
     public Player() {}
 }


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MyClass : MonoBehaviour
 {
     public List<Player> players;
     public Player current;
     public int index = 0;
 
     // Use this for initialization
     private void Start()
     {
         players= new List<Player>();
         for (int i = 0; i < 10; i++)
         {
             players.Add(new Player());
         }
         current = players[index];
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             index++;
             current = players[index];
         }
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             index--;
             current = players[index];
         }
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by RudyTheDev · Mar 05, 2015 at 09:47 PM

That has to do with the way Unity displays and then checks the edit fields for the default inspector. Since you have 2 fields for the same value -- they interfere. There are two scenarios (roughly):

  • You edit first field. Unity sees first field, sets value. Unity sees second field, sets value.

  • You edit second field. Unity sees first field, sets value. Unity sees second field, sets value.

So, no matter what you do, in the end, it just grabs the second field's value as the last.

Swap the order of public List players; and public Player current; and you'll see that you won't be able to "edit" the current anymore, because list's entry is last.

What you have would work for a custom inspector (`CustomEditor`), where you could manually go field by field and immediately update the value so any further fields have the new value instantly. For example, this would work from both fields:

 value = EditorGUILayout.IntField(value);
 value = EditorGUILayout.IntField(value);

However, Unity by default internally does closer to SerializedObject and such, which do things "in bulk" and fail under certain scenarios, like this one.

I would suggest not pointing to the same value with default inspectors. Perhaps:

 [NonSerialized]
 public Player current;
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 aga · Mar 06, 2015 at 01:03 AM 0
Share

Brilliant! That makes perfect sense, thank you!

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

2 People are following this question.

avatar image avatar image

Related Questions

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

C# | public variables in parent class should not show up in child class 1 Answer

Make inspector show changes of class variable value in function? 1 Answer

Multiple Cars not working 1 Answer

Automatically create enum based on class children types? (C#) 1 Answer


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