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.

Question Python code with Unity

Discussion in 'Formats & External Tools' started by fouratj, Oct 30, 2020.

  1. fouratj


    Sep 20, 2019
    Hi there,

    I'm trying to use python with Unity, the setup is fine and I was able to execute some script on Unity, but actually i'm trying to execute a simple code, that will look for a GameObject and assign to its child a color
    Code (CSharp):
    1. import UnityEngine as ue
    3. go = ue.GameObject.FindGameObjectWithTag("CubesManager")
    4. if go:
    5.     meshRenders = go.GetComponentsInChildren<ue.Renderer>()
    6.     #if len(meshRenders) > 0:
    7.     #    for ren in meshRenders:
    8.     #        ren.material.color = ue.Color(1, 0, 0)
    But i keep receiving the following error PythonException: TypeError : 'bool' object is not iterable
    Does anyone know how to solve this issue ? Is python not recognizing that the function return a list/tuple ?
    I tried also the following line but same thing
    Code (CSharp):
    1.     meshRenders = go.GetComponentsInChildren<ue.Renderer>()
    Any help would be appreciated

    Last edited: Oct 30, 2020
  2. fouratj


    Sep 20, 2019
    For those who will be facing this kind of issue in the future, Python doesn't seem to recognise the template function call, so instead i used the simple one. Here's how the code loook like

    Code (CSharp):
    1. import UnityEngine as ue
    2. import random as ran
    4. go = ue.GameObject.FindGameObjectWithTag("CubesManager")
    5. if go:
    6.     meshRenders = go.GetComponentsInChildren(ue.Renderer)
    7.     if meshRenders:
    8.        for ren in meshRenders:
    9.            r = ran.random()
    10.            g = ran.random()
    11.            b = ran.random()
    12.            ren.material.color = ue.Color(r,g,b)