script not returning values in if statements nested in for loop
Hello, I am having trouble with this script. First off you will notice I have a few things referenced in this that are not being used at the moment pleases ignore these for the time being.
What I am trying to do is, take a building that is set to its shader color and change its color to the tank that enters its overlapShere. After it has been "capturing" for 30f. For some reason when my if statements are nested in the for loop I am not returning any values to add up to the 30f. When moved out of the for loop they return the values but I have trouble referencing when the tank ridgid body is in the overlapSphere. this is probably the biggest nube question of the year but any help would be greatly appreciated.
P.S. since the code is sitting in private void OnTriggerEnter(Collider other) I have tried placing the if statements in a void OnTriggerStay(Collider other), my understanding is that if OnTriggerEnter detects a ridgid body the OnTriggerStay should be activated in code and play out the code inside. I was returned with the same issue though (if statements not returning any values +/- for m_capturing) . So any feedback on what i'm doing wrong with that, or if this is the wrong approach would be great to know!
Thank you!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class BuildingCapture : MonoBehaviour {
public Object Building; // Building Object
public Color ColorStart = Color.gray; // keep building at grey
public Color ColorEnd = Color.red; // Set building color to red once tank is found
public Renderer rend;
public float m_CaptureRadius = 30f; // distance of capture area
public float m_CaptureTime = 20f; // capturing time needed before captured
public float m_Capturing = 0f; // Active Capturing time
public Slider m_CaptureSlider; // slider indicating capture owner
private float m_CurrentCapturing;
public LayerMask m_TankMask;
public float duration = .5f;
public Color m_PlayerColor = Color.red;
[HideInInspector] public int m_PlayerNumber; // player owner of building
[HideInInspector] public GameObject m_Instance; //
public Vector3[] m_tankColorV;
public Vector2[] newt;
bool capturing = false;
public Shader shader;
void Start()
{
rend = GetComponent<Renderer>();
}
private void OnEnable()
{
m_CurrentCapturing = m_Capturing;
}
private void OnTriggerEnter(Collider other)
{
// Creates the Collider Sphere
Collider[] colliders = Physics.OverlapSphere(transform.position, m_CaptureRadius, m_TankMask);
// Checks entire range of colliders array (Sphere)
for (int i = 0; i < colliders.Length; i++)
{
// find their rigidbody
Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody>();
if (!targetRigidbody) // if targetRigidbody not there
continue;
capturing = true;
if (capturing == true)
{
while (m_Capturing <= 30)
{
m_Capturing += 1f;
}
}
else if (capturing == false)
{
while (m_Capturing >= 1)
{
m_Capturing = 0f;
}
}
else if (m_Capturing == 30f)
{
Renderer rend = GetComponent<Renderer>();
rend.material.shader = Shader.Find("Standard");
rend.material.SetColor("_Color", Color.red);
}
//m_Capturing += 1f;
}
}
Answer by saschandroid · Nov 20, 2015 at 12:18 PM
In line 60 you are setting capturing = true;
so the first if(capturing==true)
(line 62) is ALWAYS entered and both else if
can never be reached.
Also worth mentioning that even with line 60 removed, the third clause will never be reached. The first 2 clauses ( if (capturing == true)
and else if (capturing == false)
) encompass all possibilities.
So shouldn't it start incrementing? Since the code is true it should run through at least the m_Capturing += 1f; When in the for loop it returns no value to m_Capturing. i added capturing = true; to be sure it was hitting that case for testing purposes. But was unaware of the logic loop being incorrect I'll be fixing this. ' capturing = true;
if (capturing == true)
{
while (m_Capturing <= 30)
{
m_Capturing += 1f;
} '
Yes it will start incrementing. As it is, so long as there's an overlapping collider with a rigidbody, it will always increment (until m_Capturing == 30, after which it will do nothing).
I would recommend adding some logging so that you know where it's getting to in the function and what the important values are (for example, how do you know that there are any overlapping colliders with rigidbodies?).
And fixing the logic, obviously.