Hi all
Im trying to access a SphereCollider that i added to my gameobject by using the RequireComponent. What i essentially want to do is set the colliders radius to my range variable as shown in the code below.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent (typeof (SphereCollider))]
public class tower : MonoBehaviour {
public float towerRange;
public float deployTime;
public List<GameObject> towerTargets;
// Use this for initialization
void Start () {
SphereCollider towerRangeCollider = this.GetComponent<SphereCollider>();
towerRangeCollider.name = "Tower Range Collider";
towerRangeCollider.radius = towerRange;
towerTargets = new List<GameObject>();
}
// Update is called once per frame
void Update () {
}
// Add enemies to the towers list of targets upon entering its range
void OnTriggerEnter(Collider possibleTarget)
{
if (possibleTarget.tag == "Enemy" || possibleTarget.tag == "Friend")
{
towerTargets.Add(possibleTarget.gameObject);
Debug.Log(possibleTarget.gameObject.name + " entered the trigger");
}
}
// Remove enemies from the towers list of targets upon leaving its range
void OnTriggerExit(Collider possibleTarget)
{
if (possibleTarget.tag == "Enemy" || possibleTarget.tag == "Friend")
{
for(int i = 0;i < towerTargets.Count; i++)
{
if(towerTargets*.gameObject.name == possibleTarget.gameObject.name)*
-
{* -
towerTargets.RemoveAt(i);* -
i--;* -
}* -
}* -
}* - }*
}
... That was.. embarrasing. Thanks for the tip on the foreach loop :)
– AronChanNo problem =)
– GameVortexAbout the SphereCollider. It just doesnt seem to pick up the changes i do on my start() Ive updated the question if you wouldnt mind taking a quick look at it.
– AronChanIt should work fine as far as I can tell. When and to what do you set the value of towerRange? Also, when you say it does not pick up the changes, do you mean that it is created with a SphereCollider with default radius of 0.5 and it does not change?
– GameVortexthe goal is that whenever the prefab of the "tower" is instantiated it will attain the spherecollider and radius of the range attribute. atm i just have a cube with the script attached for testing purposes. but it should be able to change its radius whenever the radius attribute is changed.
– AronChan