I am new to Unity, and I’m working in C#.
I created a plane. On this plane are two objects. Object #1 which has a script called myReverbNode, and object #2 which has a script called myAudioController. myAudioController Checks its object’s vicinity for objects with the myReverbNode script.
I am using Physics.OverlapSphere to locate objects within a radius. Once this is done, I want to use the array it returns to test the objects that were found for the presence of the myReverbNode script.
However, although an object with the myReverbNode script is within the radius, myAudioController keeps telling me that var myScript is null.
I’m possibly being retarded on multiple levels, as I’m new to Unity. But, please take a look at my script, and see if there are any obvious problems/things I didn’t take into account. All help/tips/advice is welcome, and if I’ve left out vital information, let me know.
myAudioController.cs
float myRadius = 250;
void Update ()
{
Collider[] objectsHit = Physics.OverlapSphere(transform.position, myRadius);
int a = 0;
while (a < objectsHit.Length)
{
var myScript = objectsHit[a].GetComponent<myReverbNode>();
if (myScript != null)
{
//Do stuff.
}
if (myScript == null)
{
Debug.Log("myScript is null.");
}
a++;
}
}