Question by
Leaoi · Jul 30, 2016 at 08:10 AM ·
script.callsamedifferent objects
call same script on different objects
I have three spheres: sphereA, sphereB, sphereC; all of them share the same scriptA. then i have a square with scriptB attached.
When the square collides sphereA it returns the name "sphereA", if it collides sphereB, is "sphereB" and so on.
but i want you guys to tell me if my code will give problems or if there is an easier method, because it works fine.
scriptB
public string myname;
void OnTriggerEnter(collider oncoll)
{
if(oncoll.gameobject.tag == "square")
{
myname = gameobject.name;
}
}
void OnTriggerExit(collider offcoll)
{
if(offcoll.gameobject.tag == "square")
{
myname = null;
}
}
scriptA (the only problem is that it doesnt return null when there is no name on scriptB)
public scriptB[] scriptInfo;
public string[] sphereNames = {"sphereA","sphereB","sphereC"}
public string actualSphereName;
void Start()
{
scriptInfo = FindObjectsOfType<scriptB>();
}
for(int scriptsthere = 0; scriptsthere < scriptInfo.Length; scriptsthere++)
{
for(int names = 0; names< sphereNames.Length; names++)
{
if(scriptInfo[scriptsthere].myname == sphereNames[names])
{
switch (scriptsthere)
{
case 0:
actualSphereName = scriptInfo[scriptsthere].myname;
Debug.Log("test1");
break;
case 1:
actualSphereName = scriptInfo[scriptsthere].myname;
Debug.Log("test2");
break;
case 2:
actualSphereName = scriptInfo[scriptsthere].myname;
Debug.Log("test3");
break;
default:
actualSphereName = null;
break;
}
}
}
}
if there is any easier or safest method i would be happy to know.
Comment