title: Need a Function to Return Multiple Types of Game Objects That I Can Call Methods On
body: <p>I want to have a function return a generic game object that I can then call a method on. This doesn't work.</p>
  
  <p>I have different objects in my game world. I'd like to have one function I can call that will return the object that was found at a given location. Then, check what type of object was found. Each of my objects has a GetObjectType method that returns the type of object.</p>
  
  <p>I greatly appreciate any help you can send my way. Thanks for looking.</p>
  
  <pre><code>GameObject spotObjectLocation1;
  
  spotObjectLocation1 = GetObjectAtLocation(player.transform.position 
                                            + checkDirection);
  
  // THIS IS WHAT I WANT TO DO THAT DOESN'T WORK
  if (spotObjectLocation1.GetObjectType() == "box"){  }
  
  GameObject GetObjectAtLocation(Vector3 location)
  {
      GameObject[] objects = GameObject.FindGameObjectsWithTag("boxes");
  
      for (int i = 0; i &lt; objects.Length; i++)
      {
          if (objects[i].transform.position.x == location.x 
              &amp;&amp; objects[i].transform.position.z == location.z)
          {
              return objects[i];
          }
      }
  
      GameObject[] objects = GameObject.FindGameObjectsWithTag("blocks");
  
      for (int i = 0; i &lt; objects.Length; i++)
      {
          if (objects[i].transform.position.x == location.x 
              &amp;&amp; objects[i].transform.position.z == location.z)
          {
              return objects[i];
          }
      }
  }
  
  public class ObjectBox : MonoBehaviour {    
      public string GetObjectType()
      {
          return "box";   
      }
  }
  
  public class ObjectBlock : MonoBehaviour {  
      public string GetObjectType()
      {
          return "block"; 
      }
  }
  </code></pre>