Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 KOCGI · Jan 28 at 12:29 AM · arraylistarray of gameobjects

Call function only on the Object the player is interacting with. (when there are multiple copys of the object)

Hi, I need help with a little problem.

I have several chairs in a room the player can interact with. On all of them is a copy of a sitting.cs script. The player can enter "Sitting mode" and when standing up leave "sitting mode". In my character controller I call a function when the player presses the key to stand up. However the function is not called on the gameObject the player is sitting on, but allways on the 5th in the hierarchie. I need to call the function only on the gameObject the player is sitting on.

In PlayerController I have:

         public void stopSitting()
         {
             isSitting = false;
             sitting.isInSitMode = false;
             sitting.resetC();
         }


In Sitting.cs there is:

     public void resetC()
     {
         SitCollider.SetActive(true);
     }


As said, I have a hand full of objects the Sitting.cs is attached to. How would I call the resetC function and set the bool ONLY on the one I'm interacting with. I understand that I need to check what chair I'm sitting on and than call the function only on the script copy that is on the gameObject. How would I do that? List? Would be nice to see a code example.

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

Answer by KOCGI · Jan 30 at 08:00 PM

Is it really that incredibly hard that no one in the world can tell me how to fire a function in the nearest Object I find?

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 privatecontractor · Jan 30 at 08:20 PM 0
Share

Hi man, you could do this on many ways... lots of them depending from your scene setup, and components atached to your Player and Chairs (boxes... so objects to sit...) one of them could be using Coliders atached to Player and Objects...

(1) make a TAG "chair" (2) then in your player script add:

 private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Chair")
         {
      other.GetComponent<someScriptOnYourThingToSitOn>();
         }
     }

(3) so above you will grab things which are near you and you trigger them...


Another way could be checking whole scene for GameObject with TAG... then storing they transform, and when needed checking position to all and calculating nearest...

avatar image privatecontractor · Jan 30 at 08:23 PM 0
Share

Also man try to be nice to people, everyone here will try to help you, but not if you not nice to rest. Hope you will manage to solve your issue. I remember one awesome tutorial of how to use colliders (by CodeMonkey) maybe you should spend 10 $$anonymous$$utes on it...

avatar image KOCGI privatecontractor · Jan 30 at 10:16 PM 0
Share

Thanks. In a very strange combination of level setup and events nothing was working out. But it's fixed. I removed the events that where causing the troubles.

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

150 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 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 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 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 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 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 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 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 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

A node in a childnode? 1 Answer

List of bools assigned to each Gameobject in List/Array 0 Answers

Lists and Structs instead of Arrays? 4 Answers

[INSPECTOR ARRAY] Replace 'Element 0' by 'Map 0' with auto increase 0 Answers

Array of tags, gettings tags using CompareTag 1 Answer


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