• 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 LysolPionex · May 17, 2013 at 10:00 PM · inheritancechild objectparent-childontriggerstay

OnTriggerStay() Not Detected In Child Class

I have 2 scripts, Grabable.cs and GrabableOrb.cs. The GrabableOrb class inherits from Grabable. Grabable has an OnTriggerStay() routine and GrabableOrb does not have its own, so the inheritance just lets it use Grabable's OnTriggerStay().

In my game, there is a cube that rolls around and "grabs" an orb when the cube is both colliding with it and user is pressing the mouse button. I accomplish this by making the orb a child of the cube. When the user releases the mouse button the orb goes back to being not the cube's child (is there a word for that?) and doesn't follow the cube's rotation any more.

My problem is when the cube releases the orb, it can't grab it again, until it rolls away from and back to the orb - once the orb is released, OnTriggerStay() is no longer detected.

But! I have another object GenericGrabable which just uses Grabable.cs. The cube can grab the object, release it, and grab it again - OnTriggerStay() is detected without a problem.

So, the engine stops calling the child class' OnTriggerStay(), but the engine continues calling the parent class' OnTriggerStay(). The OnTriggerStay() method is only implemented in the parent class.

Any ideas how I can make the child class behave the same way?

Thanks!

 public class Grabable : MonoBehaviour {
     private bool isGrabbed;
     private FixedJoint grabJoint;
     protected PlayerCubeManager playerCubeManager;
     
     void Start ()
     {
         playerCubeManager = PlayerCubeManager.GetSingleInstance();
     }
     
     void LateUpdate ()
     {
         if(isGrabbed && Input.GetMouseButtonUp(0) )
         {
             GetReleased();
         }
     }
     
     void OnTriggerStay(Collider target)
     {
         if(target.gameObject.name != Constants.PLAYERCUBE_NAME)
         {
             return;
         }
         
         LookGrabable();
         
         //if the cube isn't grabbing anything or is grabbing this AND it's not moving AND the player is holding the grab button
         if(  (!playerCubeManager.GetIsGrabbing() || playerCubeManager.GetIsGrabbingThis(GetInstanceID()) ) &&
              !playerCubeManager.GetIsMoving() &&
              Input.GetMouseButton(0) )
         {
             if(!isGrabbed)    //separated from other if to keep it logical in the dev's mind
             {
                 GetGrabbed(target.gameObject);
             }
         }
     }
     
     void OnTriggerExit(Collider target)
     {
         if(target.gameObject.name != Constants.PLAYERCUBE_NAME)
         {
             return;
         }
         
         LookNotGrabable();
     }
     
     protected virtual void GetGrabbed(GameObject target)
     {
         transform.parent = target.transform;    //become a child (i.e. transform depends on parent's transform)
         isGrabbed = true;
         playerCubeManager.SetIsGrabbing(true, GetInstanceID());
         LookGrabbed();
     }
     
     protected virtual void GetReleased()
     {
         transform.parent = null;
         isGrabbed = false;
         playerCubeManager.SetIsGrabbing(false);
         LookNotGrabbed();
     }
     
     protected virtual void LookGrabable () {}
     protected virtual void LookNotGrabable () {}
     protected virtual void LookGrabbed () {}
     protected virtual void LookNotGrabbed () {}    
 }

 using UnityEngine;
 using System.Collections;
 
 public class GrabableOrb : Grabable
 {
     private float intensityNotGrabable;
     private float intensityGrabable;
     private float intensityGrabbed;
     
     // Use this for initialization
     void Start ()
     {
         playerCubeManager = PlayerCubeManager.GetSingleInstance();
         
         Light pointLight = GetComponent<Light>();
         if(pointLight != null)
         {
             intensityNotGrabable = pointLight.intensity;
             intensityGrabable = intensityNotGrabable * 2;
             intensityGrabbed = intensityGrabable * 2;
         }
     }
     
     // Update is called once per frame
     void Update () {}
     
     protected override void LookGrabable()
     {
         Light pointLight = GetComponent<Light>();
         if(pointLight != null)
         {
             pointLight.intensity = intensityGrabable;
         }
     }
     
     protected override void LookNotGrabable()
     {
         Light pointLight = GetComponent<Light>();
         if(pointLight != null)
         {
             pointLight.intensity = intensityNotGrabable;
         }
     }
     
     protected override void LookGrabbed()
     {
         Light pointLight = GetComponent<Light>();
         if(pointLight != null)
         {
             pointLight.intensity = intensityGrabbed;
         }
     }
             
     protected override void LookNotGrabbed()
     {
         Light pointLight = GetComponent<Light>();
         if(pointLight != null)
         {
             pointLight.intensity = intensityGrabable;    //i.e. still grabable, but not grabbed    
         }
     }
 }
Comment
Add comment · Show 2
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 FL · May 19, 2013 at 06:23 PM 0
Share

I don't think that this is an inheritance problem. Put the method in the child class and put a debug log to seen if this is really the problem. Smells like some problem with collider/rigidbody, if this is the case, take a look in http://unitygems.com/mistakes1/ , third topic.

avatar image LysolPionex · May 20, 2013 at 04:07 AM 0
Share

And that's what I originally thought as well. But if I add the parent class as a component ins$$anonymous$$d of the child class, it works. If I use the child class, it doesn't detect the collision until I exit and reenter.

I discovered this by doing what you suggested, and then just putting a breakpoint immediately inside OnTriggerStay() and OnTriggerStay wasn't ever called until I exited and reentered. Unless, of course, I was using the parent class.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Enabling/Deactivating objects using keys. 1 Answer

Putting a Child Class into a List of Parent Type and then casting it back to a Child 0 Answers

Dialogue system with index, children, and parent. 0 Answers

inheriting a class, but wanting it to instantiate on each gameobject it's applied to? 2 Answers

When I set an object to be the child of another object, the child object does not anchor to the parent's position for about 25 seconds 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