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
5
Question by kannan21 · Jan 27, 2014 at 09:48 AM · editorserializedpropertycustomeditorserializedobjectoninspectorgui

Type casting SerializedProperty.objectReferenceValue doesn't work?

     public class Achievement
     {
        public string description;
        public bool status;
     }
     
     
     public class AchievementsManager : MonoBehaviour 
     {
        public Achievement ach = new Achievement();
     }
     
     ////////////////////////////THE PART WHERE I GET MY PROBLEM///////////////////////////////
     
     
     
     using UnityEditor;
     using UnityEngine;
     using System.Collections;
     
     [CustomEditor (typeof(AchievementsManager))]
     public class AchievementsEditor : Editor 
     {
         SerializedObject m_object;
         SerializedProperty m_achievement;
         public void OnEnable () 
         {
             m_object = new SerializedObject(target);
             m_achievement = m_object.FindProperty("ach");
         }
     
         public override void OnInspectorGUI ()
         {
             m_object.Update();
 **//////////////I cannot type cast the m_achievement.objectReferenceValue in the next line to Achievement. BUt i saw something like that in a video//////////**
             var temp = m_achievement.objectReferenceValue as Achievement;
             m_object.ApplyModifiedProperties();
         }
     
     }
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

3 Replies

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

Answer by Jamora · Jan 27, 2014 at 04:20 PM

Achievement is not a derivative of UnityEngine.Object; it derives from System.Object, and thus the compiler cannot convert them porperly.

To fix this, you have the rather strange, non-Unity way of:

 Achievement temp = m_achievement.objectReferenceValue as System.Object as Achievement;

or alternatively make your Achievement derive from ScriptableObject.

Comment
Add comment · Show 8 · 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 kannan21 · Jan 27, 2014 at 04:30 PM 0
Share

Dude, thanks a lot. That works perfectly :D

avatar image kannan21 · Jan 27, 2014 at 04:44 PM 1
Share

Oops sorry. I get this problem after using system.object.

type is not a supported pptr value.

avatar image Jamora · Jan 27, 2014 at 04:55 PM 0
Share

It was the non-Unity way so was to be expected something would break... Seems like the only way now is to make the Achievement-class a ScriptableObject

 public class Achievement : ScriptableObject

This also means you must create each instance of Achievement with ScriptableObject.CreateInstance();

avatar image kannan21 · Jan 27, 2014 at 04:57 PM 0
Share

Can you give me a small example. That would be really helpful.

avatar image vexe · Feb 20, 2014 at 09:08 AM 2
Share

Why do you need to cast? I ran into this problem where I wanted to add an item to a serialized property list (of System.Object values) - but then I found out that I don't need to explicitly create an object and add it to the list, I could just do list.arraySize++;

If you need to perform some kind of operation on your object, then consider using target ins$$anonymous$$d, after you're doing do a serializedObject.Update(); (or whatever SerializedObject your property lives in) - you might have to register undo manually then, but it works.

Show more comments
avatar image
4

Answer by pl7890 · Apr 24, 2016 at 08:02 AM

You can get the property from the target:

 Achievement  temp = (AchievementsManager)target.ach as Achievement;

After your operations on temp, you can serialize target again and copy the value to serialzedObject:

 serializedObject.CopyFromSerializedProperty(new SerializedObject(target).FindProperty("ach"));
Comment
Add comment · 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
0

Answer by Billy4184 · Dec 13, 2019 at 10:38 PM

If you just want to display the properties, you can use:

 SerializedProperty descriptionProperty = m_achievement.FindPropertyRelative("description");
 EditorGUILayout.PropertyField(descriptionProperty);
 
Comment
Add comment · 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

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer

Editor: How to do PropertyField for List elements? 4 Answers

Editor Targets are updated but SerializedPropertys arent 1 Answer

Can you have custom EditorGUILayout fields? 0 Answers

Changing Inspector's Serialized Property Label (With Code Sample) 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