- Home /
How to know if a Script exists on another game object? C#
Hello,
I am wanting to find if a script exists on any game objects that come into my sphere of force (Physics.OverlapSphere)...
I first Find ALL GAME OBJs in a sphere around me of 20.0f.. Then i am CHECKING if those objects FOUND can be USED to eventually be MOVED UP... This check is the "bool ObjectInteractable(GameObject obj)"
THEN If an obj Found is Deemed NOT WORTHY to be moved i.e. "its out side the camera view" "Occulled from the camera" "Or simply doesnt have a rigidbody"
if the obj is NOT deemed worthy I Return FALSE. Which means that Obj Does NOT get to move!...
MY ISSUE: I want to add ONE more Parameter, to my "See if this object is DEEMED WORTHY" I want to Find if it has a script called "InteractableScript" I CANT FIGURE OUT HOW TO DO THIS?? It keeps giving me the error that it cant find it, I KNOW it cant find it, thats why im checking!
CODE (C#):
void StageOneLevetate() {
Collider [] ObjsInSphere = Physics.OverlapSphere(transform.position, 20.0f);
for(int i = 0; i < ObjsInSphere.Length; ++i) {
GameObject gObject = ObjsInSphere[i].transform.gameObject;
//For EACH Obj that is in view wich ones have a TRUE on their little script attached to them that i got from UNITY answers...
if (ObjectInteractable(gObject) == false) continue;
if (gObject.rigidbody.mass < 1.0f) {
gObject.rigidbody.velocity = Vector3.up * Random.Range(1.0f, 2.0f);
gObject.rigidbody.AddTorque(new Vector3(Random.Range(-0.02f, 0.02f), Random.Range(-0.02f, 0.02f), Random.Range(-0.02f, 0.02f)));
}
else if (gObject.rigidbody.mass < 5.0f) {
gObject.rigidbody.velocity = Vector3.up * Random.Range(3.0f, 4.0f);
gObject.rigidbody.AddTorque(new Vector3(Random.Range(-0.01f, 0.01f), Random.Range(-0.01f, 0.01f), Random.Range(-0.01f, 0.01f)));
}
}
}
//THIS Will go inside EACH
bool ObjectInteractable(GameObject obj) {
if (obj.rigidbody == null) return false;
if (obj.GetComponent<InteractableScript>() == null) return false;
// ignore objects not in the view frustum.
if (GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(Camera.main), obj.collider.bounds) == false) {
return false;
}
// ignore objects that are occluded from our view.
RaycastHit hit;
if(Physics.Linecast(transform.position, obj.transform.position, out hit)) {
if (hit.transform != obj.transform) return false;
}
return true;
}
Thanks so much for ANY help!! Daniel
I take the time to rate and accept good answers!
A side note: you may want to cache your GetComponent checks for performance reasons, as I assume this code is run from FixedUpdate() and it can clearly return multiple objects to check each time.
Assu$$anonymous$$g you don't dynamically add or remove InteractableScript from objects you can store the GetComponent results in a Dictionary... then if it an object exists in the Dictionary check the bool, if it doesn't call GetComponent and store the result.
Finding a component given a specific gameobject is not that much of a problem, especially since in advance you limit the number of searches to only the objects around the player, and only those that have a collider.
I'd call this premature optimization and keep the code simple rather than add a dictionary which will take up memory. If and only if you have some performance issues, I'd try to disable the GetComponent and see if it actually helps performance at all, and only if it does bother with creating a dictionary. $$anonymous$$y guess would be that it won't.
Thank you @tbkn your so right! UpRate my question if you like it :D Thanks
Right, I actually agree... what I meant to say was "if" you find this to be a performance issue you should consider caching the results. $$anonymous$$eeping it simple is a much better idea if it's not a noticeable drain on performance.
Answer by Daniel G · Nov 27, 2013 at 03:41 PM
My error was this... It was looking for a "TYPE" of script with the and no "" But all i wanted was to look for a NAME of a script...
Which is below using ("THE NAME OF THE SCRIPT")...
if (obj.GetComponent("InteractableScript") == null) return false;
Thanks for all the help!! Daniel
Answer by Moor · Nov 27, 2013 at 04:30 AM
just this is enough
if (obj.GetComponent<InteractableScript>) return false;
It needed to be this, Close though thanks!!
if (obj.GetComponent("InteractableScript") == null) return false;
This:
obj.GetComponent<InteractableScript>()
is much preferred over
obj.GetComponent("InteractableScript")
For various reasons. Speed is one of them.
Answer by Tomer-Barkan · Nov 27, 2013 at 06:23 AM
You use GetComponent to get the InteractableScript
script from the object. If it doesn't exist, it returns null. So you check whether it returns null or not. If it does, then the object is not interactive. Otherwise it is. See your updated code (only changed the last return statement):
bool ObjectInteractable(GameObject obj) {
if (obj.rigidbody == null) return false;
if (obj.GetComponent<InteractableScript>() == null) return false;
// ignore objects not in the view frustum.
if (GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(Camera.main), obj.collider.bounds) == false) {
return false;
}
// ignore objects that are occluded from our view.
RaycastHit hit;
if(Physics.Linecast(transform.position, obj.transform.position, out hit)) {
if (hit.transform != obj.transform) return false;
}
// return true if object has InteractableScript, otherwise return false
return (obj.GetComponent<InteractableScript>() != null);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Have null errors 1 Answer
Distribute terrain in zones 3 Answers
XMLDocument GetElementByID returning null 1 Answer
Confused as to why I'm getting a NullReference Exception 1 Answer