- Home /
Can't use normal class in Unity?
I'm trying to add threading to my game. I know that Unity is not thread-save so I want to have the thread in an external class (without MonoBehaviour). But for some reason, the object is just working when I make it a MonoBehaviour. If I don't, the class just won't execute, althought there's no error in my script. It's just not executing. I am using the new-operator to create a instance of the class.
What do you mean by "the class just won't execute"? Its not supposed to execute on its own.
First, just in case, check if your object is created, something like:
myClass object = new myClass();
Debug.Log(object);
If that is not null, your class instance is set up just fine. That means you can just call whatever methods you have defined... Like
object.DoSomething();
object.foo(bar);
Also, it would help if you showed some code, so that we could look at the problem ourselves.
It's Boo code, but since Boo and C# are the same bytecode, it's not language related. For better understanding, I'll translated the code to C#: $$anonymous$$onoBahaviour:
//other class stuff
ClientScript client = new ClientScript();
client.Start();
print(client);
print("test");
Threadclass:
public class ClientScript {
public void Start () {
print("class there!");
myThread = new System.Threading.Thread(run);
myThread.run();
}
void run () {
print("thread activated");
}
There aren't any errors and client is not null, but the console just prints out the client and "test", not more...
Answer by Jeyex · Oct 17, 2014 at 07:00 PM
100% my fault. "print" is a Unity function. It works with Debug.Log().
Glad you solved your problem :) Also, you can use Unity functions in regular classes, just add using UnityEngine;
, I do that very often, so I just overlooked the fact that you didn't.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Jobs running over multiple frames 1 Answer
How to realize accurate time counter (millisecond precision) 3 Answers
What threads are used in a scene? 1 Answer