Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Resolved Access Non MonoBehavior via MonoBehavior?

Discussion in 'Scripting' started by renman3000, Mar 8, 2023.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Hi there,
    I have a third-party asset. Its components are not derived from Monobehavior, but instead from another. The thing is, I want to access these scripts but from MonoBehavior.
    How can I do this?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    Really depends on the context. But, for example, if they were regular C# classes, you'd just declare one in the body of your monobehaviour class and use that.

    More context is needed though to give a specific answer.
     
    renman3000 likes this.
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681

    Ok,
    so I have a third party asset. Its components, allow me to manipulate transforms positions, using gears and joints extra. It is very handy, on its own.

    However, I want to be able to access particular public variables in one of these scripts, so I can manipulate the values, more easily.

    This is what a script looks like...
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5.  
    6.  
    7. [ExecuteInEditMode]
    8. public class IKAxis : IKBase{
    9. public float someFloat;
    10. }
    11.  

    I want to be able to access that someFloat. But since it is in IKBase, I can not.

    How can I?
    Thanks
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    I think you have a misunderstanding about inheritance here? IKAxis might inherit from IKBase, but IKBase probably inherits from monobehaviour (or another that, eventually, inherits from monobehaviour).

    So it's just a component, which you can reference via the inspector.
     
  5. dillbacon55

    dillbacon55

    Joined:
    Mar 24, 2014
    Posts:
    7
    Also, what exactly do you mean by this? If the variable is public, you can access it via script outside of the class. Did you try simply changing the value through code?
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681

    Ok....
    So it (Visual Studio) was not seeing the class, why I wrote... but after shutting it down and restarting, it does see it, so problem solved.

    Thanks!