- Home /
Giving a public Transform field a default value in the inspector
I have a field called "centerPoint" which stores a the transform of a "point placer" object. I want centerPoint to be able to be changed in the inspector by dragging the "point placer" object into the inspector, but I also want centerPoint to "default" to the objects own transform. Essentially, I want to do this.
public abstract class Enemy : MonoBehaviour {
public Transform centerPoint = transform;
}
But then, as expected, I get this error:
Assets/scripts/MonoBehaviors/Characters/Enemy.cs(11,40): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
Obviously, this is happening because the value of "transform" will change during runtime. Is there any way that I can get around this while still showing it in the inspector?
Answer by DannyB · Jun 27, 2013 at 06:52 PM
How about
public Transform centerPoint = null;
And then
void Awake() {
if( centerPoint == null ) centerPoint = transform;
}
Your answer
Follow this Question
Related Questions
Use via Inspector to script assigned public texture as default 0 Answers
Removing transform from Inspector 1 Answer
After update of Unity Editor all objects in scene lost references to any scene object. 2 Answers
hashtable as public to show in the inspector? 1 Answer
I can modify the GameObject transform using the inspector, how can I modify the mesh as well? 0 Answers