Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved GetComponent a script based on it's parent script

Discussion in 'Scripting' started by MediocreDude, May 8, 2022.

  1. MediocreDude

    MediocreDude

    Joined:
    Nov 12, 2020
    Posts:
    1
    I have this code for getting the gameobject of something that has been left-clicked attached to the main camera, and would like to call a click handling script specific to the gameobject.

    I tried making a parent class that extends monobehavior, then having a child class that extends the parent. I then attached the child class to the gameobject and tried calling GetComponent(parent) which just returns null every time. Is there any way to use GetComponent to return the child of the class I pass into it?

    Edit: Think I found a workaround
     
    Last edited: May 8, 2022