- Home /
How do i access a SphereCollider added by RequireComponent?
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[i].gameObject.name == possibleTarget.gameObject.name)
{
towerTargets.RemoveAt(i);
i--;
}
}
}
}
}
Answer by GameVortex · Nov 18, 2013 at 01:25 PM
Anywhere within a function in your Tower class you can access the SphereCollider by doing this:
SphereCollider sphereCollider = GetComponent<SphereCollider>();
You can use that in the start function to apply the radius.
To your side note question:
The OnTriggerExit will not be called at all because it has to be written with a upper case 'O' instead of a lower case 'o'.
Also you should not remove objects from a List inside of a foreach through that same list, because the foreach will then fail on the potential next iteration in the list. I usually do it like this if I need to remove from a list I am iterating through:
for(int i = 0; list.Count; i++)
{
if(shouldBeRemoved)
{
list.RemoveAt(i);
i--;
}
}
... That was.. embarrasing. Thanks for the tip on the foreach loop :)
About the SphereCollider. It just doesnt seem to pick up the changes i do on my start()
Ive updated the question if you wouldnt $$anonymous$$d taking a quick look at it.
It 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?
the 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.