Skip to content

Commit

Permalink
Updated some backing field direct usages with their properties. Repla…
Browse files Browse the repository at this point in the history
…ced some GetComponent calls with TryGetComponent.
  • Loading branch information
jadvrodrigues committed Aug 29, 2023
1 parent 2356725 commit d851d78
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions Assets/CustomNavMesh/Scripts/HiddenNavMeshAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public bool IsBlocking

if (destination.HasValue)
{
agent.SetDestination(destination.Value);
Agent.SetDestination(destination.Value);
}
}
}
Expand Down Expand Up @@ -121,8 +121,7 @@ NavMeshAgent Agent
{
if (agent == null)
{
agent = GetComponent<NavMeshAgent>();
if (agent == null)
if (!TryGetComponent(out agent))
{
agent = gameObject.AddComponent<NavMeshAgent>();
}
Expand All @@ -142,8 +141,7 @@ NavMeshObstacle Obstacle
{
if (obstacle == null)
{
obstacle = GetComponent<NavMeshObstacle>();
if (obstacle == null)
if (!TryGetComponent(out obstacle))
{
obstacle = gameObject.AddComponent<NavMeshObstacle>();
}
Expand Down Expand Up @@ -175,7 +173,7 @@ public bool SetDestination(Vector3 target)
SwitchToAgent();

destination = target;
return agent.SetDestination(target);
return Agent.SetDestination(target);
}

/// <summary>
Expand Down Expand Up @@ -226,21 +224,18 @@ public bool IsLinkedWith(CustomNavMeshAgent customAgent)

protected override void OnCustomEnable()
{
var meshFilter = GetComponent<MeshFilter>();
if (meshFilter == null)
if (GetComponent<MeshFilter>() == null)
{
gameObject.AddComponent<MeshFilter>();
}

var meshRenderer = GetComponent<MeshRenderer>();
if (meshRenderer == null)
if (GetComponent<MeshRenderer>() == null)
{
meshRenderer = gameObject.AddComponent<MeshRenderer>();
var meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial = CustomNavMesh.HiddenAgentMaterial;
}

obstacle = GetComponent<NavMeshObstacle>();
if (obstacle == null)
if (!TryGetComponent(out obstacle))
{
obstacle = gameObject.AddComponent<NavMeshObstacle>();
obstacle.carving = true;
Expand Down Expand Up @@ -332,8 +327,8 @@ void SwitchToAgent()
{
isBlocking = false;

obstacle.enabled = false;
agent.enabled = true;
Obstacle.enabled = false;
Agent.enabled = true;

timer = 0.0f;
GetComponent<MeshRenderer>().sharedMaterial = CustomNavMesh.HiddenAgentMaterial;
Expand All @@ -343,8 +338,8 @@ void SwitchToObstacle()
{
isBlocking = true;

agent.enabled = false;
obstacle.enabled = true;
Agent.enabled = false;
Obstacle.enabled = true;

timer = 0.0f;
GetComponent<MeshRenderer>().sharedMaterial = CustomNavMesh.HiddenBlockingAgentMaterial;
Expand Down

0 comments on commit d851d78

Please sign in to comment.