I’ve just started scripting in Unity C#, and I am having problems calling a method on a gameobject from within another script on a different gameobject. Searched Google and these forums, but to be honest I’m not sure what I should be looking for in the first place.
I’ve got the player, a “Mother” of sorts, that can check the surrounding area for children that should then start following the mother character. When this call is made, CallChildren() is called.
In CallChildren, I go through all found colliders in the sphere, and if the tag matches with “child” I try to call for a method (setFollowing()) inside a script that is assigned to that gameobject.
void CallChildren (Vector3 center, float radius) {
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
int i = 0;
while (i < hitColliders.Length) {
print (hitColliders*.gameObject.transform.name);*
_ if (hitColliders*.gameObject.transform.tag == “child”) {_
_ print (“Found a child!”);_
_ GameObject foundCollider = hitColliders.gameObject;
foundCollider.setFollowing(); // This is not working!
}
I am getting this error:
Assets/Scripts/Player/Script_Player_Controls.cs(98,47): error CS1061:
``Type UnityEngine.GameObject' does not contain a definition for
setFollowing’ and no extension method setFollowing' of type
UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)*
How can I call for the method I need to make the child object follow my player character?
I guess GetComponent has something to do with this, but I have no idea how to use it here._