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 DalinCriid · Jan 21, 2014 at 11:12 AM · c#editoreditor-scripting

Modify GameObject Components via C# Reflection

Hi,

I am currently trying to develop a tool for Unity like Animation and I want to use the C# Reflection to make it dynamic.

Up to now I've been able to get these Properties and to read their values but I don't succeed in modifying its values.

I use the PropertyInfo.SetValue method to modify the Transform Component of this GameObject, the data is correctly modified but my GameObject does not move and the values in the Inspector do not change. If i get my value back right away it's still the one i put before If i get my GameObject and check its transform, it's changing as i want too (but still not in the scene or in the inspector).

IMPORTANT: I'm doing it in editor mode, without inheriting from MonoBehaviour or Editor (but still when i get the GameObject it has my modifications).

Can anyone point me on the right direction about that ? Thank you !

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

2 Replies

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

Answer by DalinCriid · Jan 21, 2014 at 03:30 PM

Ok, It looks like some properties cannot be modified this way, for example the transform.position cannot but the transform.localPosition does the trick.

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 Kuertz · Jan 22, 2014 at 09:11 AM 0
Share

transform.position property info is : PropertyInfo.CanRead && PropertyInfo.CanWrite but it still not editable.

avatar image
1

Answer by HarvesteR · Aug 18, 2015 at 02:55 PM

I'm not sure if Reflection is the best approach here. I'm assuming you want to have control over GameObject properties without having to attach MonoBehaviours to the objects themselves?

I would suggest having a look at C#'s extension methods. You can write a class (not mb-subclass, mind) with methods like this:

 public static class GameObjectExtension
 {
     public static void DoStuffMyWay(this GameObject go)
     {
          // do stuff to 'go' your way
     }
 }

And that will appear as if it were a member of GameObject, meaning you get to call it like this:

 GameObject myObj;
 
 //...inside some method
 
 myObj.DoStuffMyWay();


I'm not sure what exactly you're trying to accomplish, but if you want to add extra methods to existing types without subclassing them, extension methods are the way to go!

Cheers

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

20 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

Related Questions

Bind to OnWillSaveAssets and force to save my scene 0 Answers

Updating object on inspector value changes in editor 1 Answer

object field decides when it wants to work,Why does my list clear itself 0 Answers

Multiple Cars not working 1 Answer

DestroyImmediate(component.gameObject) destroys component but not gameObject 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