Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrRightclick · Jul 29, 2013 at 04:29 PM · scriptingbasicsoverlapsphere

Calling a script from other GameObject after finding it using OverlapSphere

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[i].gameObject.transform.name);
             if (hitColliders[i].gameObject.transform.tag == "child") {
                 print ("Found a child!");
                 GameObject foundCollider = hitColliders[i].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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Stevenwithaph · Jul 29, 2013 at 05:14 PM

You're right about using GetComponent

 foundCollider.GetComponent<ClassName>.setFollowing();

and replace ClassName with whatever the class name is for the children.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrRightclick · Jul 29, 2013 at 05:19 PM 0
Share

Thanks for the answer!

I tried this:

 foundCollider.GetComponent<Script_Child_Follow>.startFollowing();

but got this error on the modified line: Assets/Scripts/Player/Script_Player_Controls.cs(98,47): error CS0119: Expression denotes a method group', where a variable', value' or type' was expected

Am I doing something wrong here?

avatar image ChrisWoo · Jul 29, 2013 at 05:23 PM 0
Share
 foundCollider.GetComponent<Script_Child_Follow>().startFollowing();
 
 You missed the () after <Script_Child_Follow>

since GetComponent is a generic method so it still requires the ().

avatar image MrRightclick · Jul 29, 2013 at 05:25 PM 0
Share

Ah, that did it. I just copied and pasted the line in Steven's answer, and it didn't seem to have the () included. It's working now, thank you for the answers!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Why does "Missing (Mono Script)" keep happening? 3 Answers

Material doesn't have a color property '_Color' 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges