Question by
unity_5xUL_9e65yZg0w · Nov 23, 2021 at 12:51 AM ·
gizmos
Is it possible to get the transform for Gizmos from the start method to keep the Gizmo's transform constant during runtime?
I created an enemy who can only move within a certain distance of their start location. I want to show that distance using OnDrawGizmos with the below code. But when I use the below, I get "NullReferenceException: Object reference not set to an instance of an object". It looks like it happens because the start method is not called before running the game. When I run the game, the cube follows the enemy around instead of staying in the initial enemy position. Is there a way to get the cube to stay at the initial enemy position instead of following the enemy?
private Transform initialPosition;
public float walkPointRange;
void Start()
{
agent = GetComponent<NavMeshAgent>();
initialPosition = transform;
}
void OnDrawGizmos()
{
Color colliderColor = Color.green;
colliderColor.a = 0.2f;
Gizmos.color = colliderColor;
Gizmos.DrawCube(initialPosition.position, new Vector3(walkPointRange, 1, walkPointRange));
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613045709im_/https://answers.unity.com/themes/thub/images/avi.jpg)