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 Mariiooo · Dec 30, 2021 at 02:35 PM · triggertriggers

Help with OnTriggerEnter. Code inside trigger is never going off.

Hello,,

I want my player to be able to do a 180 flip when he is near a chair. So that he will sit baiscly.
I'm using the OnTriggerEnter and Exit just like that:

 public class ChairScript : MonoBehaviour
 {
     public ExtraPlayerButtonConfiguration extraPlayerButtonConfiguration;
 
     public void Start()
     {
         extraPlayerButtonConfiguration = gameObject.GetComponent<ExtraPlayerButtonConfiguration>();
     }
 
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
           if (other.gameObject.TryGetComponent(out ChairScript chairScript))
           {
                 extraPlayerButtonConfiguration.closestChair = this;
             }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             if (other.gameObject.TryGetComponent(out ChairScript chairScript))
             {
                 if (extraPlayerButtonConfiguration.closestChair == this)
                 {
                     extraPlayerButtonConfiguration.closestChair = null;
                 }
             }
         }
     }
 }


And I'm calling it inside this function:

 public class ExtraPlayerButtonConfiguration : NetworkBehaviour
 { 
     [SerializeField]
     private GameObject playerXRRig;
 
     private InputAction sitOnChair;
 
     private Transform chairTransform;
     private Transform playerTransform;
 
     public ChairScript closestChair;
 
     // Start is called before the first frame update
     void Start()
     {
         var gameplayActionMapLeftHand = playerControls.FindActionMap("XRI LeftHand");
         sitOnChair = gameplayActionMapLeftHand.FindAction("Sit");
         sitOnChair.performed += OnSitOnChair;
         sitOnChair.Enable();
     } 
 
     //Sit On Chair Action calling.  
     public void OnSitOnChair(InputAction.CallbackContext context)
     {
         if (IsClient && IsOwner)
         {
             if (closestChair)
             {
                 //  Rotate the player and snap him to the chair
                 playerTransform = chairTransform.transform;
                 playerTransform.rotation *= Quaternion.Euler(0, 180f, 0);
                 playerTransform.position = chairTransform.position - new Vector3(0, 0, 1.0f);
             }
         }
     }


But my code never enter inside

     if (other.gameObject.TryGetComponent(out ChairScript chairScript))
            {
                  extraPlayerButtonConfiguration.closestChair = this;
             }


I have no clue what I'm doing wrong or why it doesn't want to work. Could it be that the second script is NetworkBehaviour because I'm using Netcode for GameObject for my multiplayer VR app? Or what could be the problem?

alt text

trigger1.jpg (235.0 kB)
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 henkehedstrom · Jan 07 at 11:05 AM

Does your player object have the ChairScript object because if it doesn't it wont enter that if statement. Since you are not using that ChairScript component inside the if function you might not be able to need that check at all unless you remove and add the chaircomponent on the player during runtime.

Btw instead of using other.tag you could use the compare function: https://docs.unity3d.com/ScriptReference/Component.CompareTag.html . Might not have a big impact on performance on your current project but is good practise to use that becaue it allocates less memory.

Comment
Add comment · Show 1 · 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 Mariiooo · Jan 07 at 04:06 PM 0
Share

Tnx for the answer.. I have fixed this issue in meantime.. I made a mistake and connected the wrong scripts.. This is the right answer and it's working.

 using UnityEngine;
 
 public class ChairScript : MonoBehaviour
 {
     private void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             if (other.gameObject.TryGetComponent(out ExtraPlayerButtonConfiguration extraPlayerButtonConfiguration))
             {
                 extraPlayerButtonConfiguration.closestChair = this;
             }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             if (other.gameObject.TryGetComponent(out ExtraPlayerButtonConfiguration extraPlayerButtonConfiguration))
             {
                 if (extraPlayerButtonConfiguration.closestChair == this)
                 {
                     extraPlayerButtonConfiguration.closestChair = null;
                 }            
             }
         }
     }
 }

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

158 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BoxCollider2D, Trigger not working 1 Answer

OnTriggerEnter problems 3 Answers

Activate trigger if items colected 1 Answer

OnTriggerExit2D not working, but OnTriggerEnter2D does??? 5 Answers

How to Specify a Collider in OnTriggerStay function? 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