- Home /
Accessing Normal Class from another class which derives from Monobehaviour
Hi,
I Created an Normal .CS Script for getting the instance whenever i called the particular function in following script.
public class Indexer
{
private static Indexer instance = null;
public static Indexer GetInstance()
{
if(instance == null)
{
instance = new Indexer();
}
return instance;
}
}
Trying to call GetInstance() in to the Class which derives Monobehaviour like below:
private Indexer index;
public class Model : Monobehaviour
{
void Start ()
{
index = Indexer.GetInstance ();
}
}
I am doing this process when user swithces from one scene to antoher scene.(say, Menu to Game scene).
But, because of this line [index = Indexer.GetInstance ();] it hangs and crahses unity many times.
Can anyone help to solve this issue. . ?
Answer by Simon Says · Dec 17, 2013 at 06:22 AM
The posted code seems in order. It must be something in the constructor of your Indexer that crashes Unity.
Also, making the Indexer a sole public class in your Normal.cs file could help.
Answer by GameVortex · Dec 17, 2013 at 08:16 AM
That code has a few errors and would not compile.
1: You can not have global variables in C#, the line "private Indexer index;" needs to be inside the Model class.
2: MonoBehaviour is written with an upper case B.
It should work after that though.
Yeah, you're right. I assumed he already got it to compile and the issues are just typos that were introduced when posting the example here. It's good you're pointing them out, though. I didn't notice them at all at first.