Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Python for Unity doesn't support Custom class

Discussion in 'Formats & External Tools Previews' started by leeeel, Sep 13, 2020.

  1. leeeel

    leeeel

    Joined:
    Sep 13, 2020
    Posts:
    1
    Given a custom class
    public class GenericProjectile:MonoBehaviour{}

    When using Addcomponent() function, the function couldn't recognize the custom Types( if this class is not in a namespace).

    Any way to solve this problem?

    That introduce custom types to Python?
     
  2. tran10

    tran10

    Joined:
    Nov 30, 2020
    Posts:
    13
    Hello,
    I have the same problem but I didn't find the solution to use Addcomponent() function in Python.
    Hope that someone can have solution :(
     
  3. tran10

    tran10

    Joined:
    Nov 30, 2020
    Posts:
    13
    Ok, I found the solution we can use "clr" to get the environment of Unity like this:
    Code (Boo):
    1. import clr
    2. print(dir(clr))
    3. import UnityEngine as UE
    4.  
    5. go = UE.GameObject.CreatePrimitive(UE.PrimitiveType.Cube)
    6. go.AddComponent(clr.GenericProjectile)
    7.  
     
    fangye_Studio and LeonLiiAng like this.
  4. fangye_Studio

    fangye_Studio

    Joined:
    Nov 19, 2012
    Posts:
    26
    brilliant, this Unity Python really lacks of good documents , I think all user must to read the http://pythonnet.github.io/ very carefully ,hope they can provide a rich document later soon