Hello, I am new to Unity and I was wondering if anybody could help me.
My problem is that i have no idea how to change the radius of the sphere collider in C#. If anybody could help it would be much appreciated.
SphereCollider myCollider = transform.GetComponent<SphereCollider>();
myCollider.radius = 10f; // or whatever radius you want.
further reading:
http://unity3d.com/support/documentation/ScriptReference/SphereCollider-radius.html
You must typecast the collider to SphereCollider, like below:
(collider as SphereCollider).radius = 5;
The same applies to the other types (BoxCollider, CapsuleCollider etc.).
SphereCollider SP = gameObject.GetComponent();
SP.radius = 3.0f;
It works in my case.
This is perfect for explosions that affect other objects around it, like to create a “blast zone”. Check out a little demo, made with the script below: Bomb Test Web Player Demo Press F5 to see it explode over and over.
using UnityEngine;
using System.Collections;
public class Bomb : MonoBehaviour
{
private SphereCollider myCollider;
public Transform particles;
private float time = 2;
void Start () {
myCollider = transform.GetComponent<SphereCollider>();
}
void Update ()
{
if (Time.time >= time)
{
if (myCollider.radius < 50)
{
Instantiate(particles, transform.position, transform.rotation);
myCollider.radius += 2f;
}
else
{
myCollider.enabled = false;
}
}
}
}