Setting GameObject properties in editor when public script fields are changed
I was wondering if it's possible to set the properties of a game object when a script on that gameobject has one of its public properties filled.
There are Unity objects that do this already. Most UI objects update in editor when their Text or Image component is changed. If that's possible for me to implement for my game objects that would be very helpful.
For example, say I had a bullet prefab that had a script component with a public damage property. It would be really helpful if when i set that variable, it affected the size/scale of the bullet.
Just to clarify, I want this to happen when the editor is NOT in play mode. It would be easy to write some code to change the size of the bullet when the game is running. I want it to change in the editor while the game is NOT running, like the UI elements I mentioned before.
Is this possible?
Thanks in advance
Answer by Santosh_Nair · Oct 14, 2018 at 02:25 PM
If I am getting your question correctly, I think let's assume when damage is 50 and you change it manually to 70, you want the size to change from 10 to 12 by itself. Assuming all these are public variables. I don't think unity will allow it unless you link them somehow using a code. And even in that case, it won't change itself inside the editor, but only recalculate and update once you press play. By the way, why do you need this?
Just a heads up. I found a unity tutorial that talks about the "ExecuteInEdit$$anonymous$$ode" attribute! If you're interested in this as well, here's the link:
https://unity3d.com/learn/tutorials/topics/scripting/attributes
Answer by hahman12 · Oct 14, 2018 at 05:06 PM
Mostly convenience.
My real example is a little different. My player character has two weapon slots. I was hoping that I could set it up so when I drag a weapon into one of the slot fields on the player script, it would instantiate the weapon in the editor at a set transform (the players hand). This way I can see the weapon equipped to the character before I press play. I'm sure you can imagine why something like that would be pretty convenient.
Otherwise I have to manually drag the prefab into the hierarchy and set it's transform, and delete it when i'm done with it. Or set it to instantiate and position itself on play, which is also a little annoying because I have to start the game just to see the result.
The bullet example was something i wanted as well, but not what inspired the question.
Oh ok. That would have been convenient, but I don't think unity has any such option, especially instantiating when we drag drop. And, If it does, then I am also unaware.
Your answer
Follow this Question
Related Questions
is there any way to select gameobject by script in hierarchy ? 0 Answers
My collectable objects don't respond like they should after calling a coroutine for the first time? 0 Answers
How to add wait for second for a Editor Script? 0 Answers
HideFlags.HideInHierarchy not updating hierarchy in Edit-mode 3 Answers
A script behaviour has a different serialization layout... 7 Answers