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*.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!

just this is enough

 if (obj.GetComponent<InteractableScript>) return false;

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);
}

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