Search Unity

How to get GameObject's script component in python file

Discussion in 'Scripting' started by unity_KMYExUC9a3rXoA, Jul 5, 2021.

  1. unity_KMYExUC9a3rXoA

    unity_KMYExUC9a3rXoA

    Joined:
    Mar 13, 2020
    Posts:
    2
    Hi,

    I just download python for unity 4.0.0-exp.5 package, and try to send a list of int to Python so it can be calculated by using numpy in Python.

    When I tried to get the script component under the game object named "main", I got an error said

    PythonException: NameError : name 'Main' is not defined
    [' File "<string>", line 6, in <module>/n'] at Python.Runtime.PythonException.ThrowIfIsNull (Python.Runtime.BorrowedReference reference) (at <5dde01e97c6f480082d83366ee8e86a6>:0)
    Python.Runtime.PyScope.Exec (System.String code, System.IntPtr _globals, System.IntPtr _locals) (at <5dde01e97c6f480082d83366ee8e86a6>:0)
    Python.Runtime.PyScope.Exec (System.String code, Python.Runtime.PyDict locals) (at <5dde01e97c6f480082d83366ee8e86a6>:0)
    UnityEditor.Scripting.Python.PythonRunner.RunString (System.String pythonCodeToExecute, System.String scopeName) (at Library/PackageCache/com.unity.scripting.python@4.0.0-exp.5/Editor/PythonRunner.cs:155)

    The "Main" is the script file attached under the game object "main" like below:
    upload_2021-7-5_19-10-20.png

    This is my code in Python Script Editor:
    Code (Python):
    1. import UnityEngine
    2. import System
    3.  
    4. main = UnityEngine.GameObject.Find("main")
    5.  
    6. l = main.GetComponent<Main>().test()
    7.  
    8.  
    The test() is a function under Main which will return a list{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} simply.

    Can anyone tell me how to define the script so I can recieve the list under this script?
     
    Mile88 likes this.
  2. Mile88

    Mile88

    Joined:
    Apr 17, 2017
    Posts:
    2
    It should be in your case
    l = main.GetComponent("Main").test()
     
    B0berchIk likes this.