How do i use GameObject.FindObjectsOfType? #17
Replies: 8 comments
-
Hi, any thoughts about this issue? I dont have any useful, so im asking) |
Beta Was this translation helpful? Give feedback.
-
@exodrifter Hi, any updates? |
Beta Was this translation helpful? Give feedback.
-
@JustClaireee No, I don't know what the answer is off the top of my head and I haven't had time yet to investigate. |
Beta Was this translation helpful? Give feedback.
-
@exodrifter Ok, ill wait for the solution and try to look into it myself) |
Beta Was this translation helpful? Give feedback.
-
@exodrifter An update on the issue! I noticed that i used triangle brackets instead of square, however it didnt fix the issues. Now the function is executed, but it also freeses unity in the process. import UnityEngine |
Beta Was this translation helpful? Give feedback.
-
Thanks for being patient! I'll do my best to help you out but I might not have the best response times as I'm not always freely available. As you mentioned in your most recent comment, you need square brackets to call generic methods. I was able to run your code after making this change, so I'm not sure why Unity freezes for you. Can you perhaps post a small, self-contained example? What version of Unity are you using? Are you using the latest version of this repository? Are you doing anything else that may cause it to freeze? Here's what worked on my machine: test.txt from System import Array
import UnityEngine
gos = UnityEngine.GameObject.FindObjectsOfType[UnityEngine.Transform]()
for GO in gos:
UnityEngine.Debug.Log('Hi from' + GO.gameObject.name) Test.cs using UnityEngine;
public class Test : MonoBehaviour
{
public TextAsset text; // text.txt
void Start()
{
var engine = UnityPython.CreateEngine();
var scope = engine.CreateScope();
var source = engine.CreateScriptSourceFromString(text.text);
source.Execute(scope);
}
} |
Beta Was this translation helpful? Give feedback.
-
@exodrifter Thanks for the help! Ive found my mistake: i was runnung python scripts for each instance of a prefab in my scene and thus creating a new script engine and a new scope for each instantiated object. It seems that it cant handle ~60 engines requesting GameObject.FindObjectsOfType at the same time. Now im creating only one engine and scope and then running the scripts for each object inside of them. It was my mistake. Sorry for causing you trouble for nothing. |
Beta Was this translation helpful? Give feedback.
-
Don't worry about it, I'm glad you found a solution! |
Beta Was this translation helpful? Give feedback.
-
When i use FindObjectsOfType which is supposed to return an array of objects in a python script, i get this error TypeErrorException: iteration over non-sequence of type bool
my code is
from System import Array
import UnityEngine
gos = UnityEngine.GameObject.FindObjectsOfType<UnityEngine.Transform>()
for GO in gos:
UnityEngine.Debug.Log('Hi from' + GO.gameObject.name)
Am i doing something wrong? In that case, how do i use UnityEngine.GameObject.FindObjectsOfType?
Beta Was this translation helpful? Give feedback.
All reactions