Sign in to follow this  

Unity CreateInstance can only be called from the main thread.

Recommended Posts

New to Unity and c# I get:

CreateInstance can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

How to rewrite and/or put into the main cs?
private BSplineMath bmath = ScriptableObject.CreateInstance("BSplineMath") as BSplineMath;


If I put that in the main class I get a reference error:

error CS0103: The name `bmath' does not exist in the current context

No wonder, how to use an instance in another class. In c++ I would only

include the header?
Many thanks in advance


Edited by FGFS

Share this post

Link to post
Share on other sites
Not familiar with Unity, but I do know C#. Based on the text in the error message you posted you want:
private BSplineMath bmath;
in your class, and then in some kind of Awake or Start function (defined by Unity most likely?) put your initialization code:
bmath = ScriptableObject.CreateInstance("BSplineMath") as BSplineMath;
Basically, you're splitting up the definition of the variable and the initialization since (apparently) Unity doesn't like you calling CreateInstance when an instance of your class is constructed.

Share this post

Link to post
Share on other sites

You need to post some code. Show the code where you are initialising bmath and the code where you want to use it.


From the error message you've posted I suspect you have something like this:

// Spline.cs

public class Spline
    private BSplineMath bmath = ScriptableObject.CreateInstance("BSplineMath") as BSplineMath;

// program.cs
public class Program
    static void Main(string[] args)

If you want to use bmath in your Main (probably a bad idea, btw) you need to get a reference to it somehow.

Share this post

Link to post
Share on other sites
So, the "main thread" is a completely different thing than "main cs". Explaining what a thread is...unfortunately a pretty complex topic and is beyond the scope of this discussion.

The correct solution in this case looks like this (like what SmkViper said):
public class YourClass : MonoBehaviour
    private BSplineMath bmath;

    void Awake() // NOTE: Unity always calls Awake on its main thread.
        bmath = ScriptableObject.CreateInstance("BSplineMath") as BSplineMath;
Edited by Nypyren

Share this post

Link to post
Share on other sites

Thanks, I didn't know about awake nor didn't have that at all. Now it compiles but at runtime I get:

NullReferenceException: Object reference not set to an instance of an object
BSplineSurface.Init () (at Assets/BSplineSurface.cs:64)

The last line below is 64. Many thanks again in advance.



public class BSplineSurface : ScriptableObject {
    //size of the control net (e.g. 3x4 net)
    public int NI = 4; //setting these to high can crash unity
    public int NJ = 4;
    //Grid of control points
    public Vector3[,] controlGrid;

    //The degree in each direction
    public int TI = 3;
    public int TJ = 3;
    //output GRID resolution (e.g. 30x40)
    public int RESOLUTIONI = 150; //setting these to high can crash unity
    public int RESOLUTIONJ = 150;
    //the output Grid
    public Vector3[,] outputGrid;
    public BSplineMath bmath;
    //internal knots in each direction
    private int[] knotsI;
    private int[] knotsJ;
    //internal variables
    private int i, j, ki, kj;
    private float intervalI, incrementI, intervalJ, incrementJ, bi, bj;

    void Awake() // NOTE: Unity always calls Awake on its main thread.
        bmath = ScriptableObject.CreateInstance("BSplineMath") as BSplineMath;


    public BSplineSurface()
    public void Init () {

        controlGrid = new Vector3[NI+1,NJ+1];
        outputGrid = new Vector3[RESOLUTIONI, RESOLUTIONJ];
        //init step size (the increment steps)
        incrementI = (NI-TI+2) / ((float)RESOLUTIONI - 1);
        incrementJ = (NJ-TJ+2) / ((float)RESOLUTIONJ - 1);
        //Calculate KNOTS
        knotsI = bmath.SplineKnots(NI, TI);

Share this post

Link to post
Share on other sites

You Init() method runs on constructions, and makes use of the "bmath" field. Since that field isn't populated until Awake() is called (which is necessarily after construction), you are getting a NullReferenceException.


Given the constraints that BSplineMath has about which thread it is called from, you'll probably need to defer calculating the spline knots until Awake() is called. At this point, your constructor / Init() method aren't really giving you a lot of value - it might make sense to put all your initialisation code in the Awake() method.


I'd also mention that some of your fields, in particular "i, j, ki, kj" look like they should probably be local variables.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By Simplepg
    • By Simplepg
    • By ForgedInteractive

      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! We're a team of artists, animators, programmers, level designers, writers, composers, producers, and other creative minds. We want to make games that you, the modern gamer want to play! We hope to build a community that enjoys our games as much as we love creating them. With your feedback and support we will be able to achieve that.

      About the Game
      GAME NAME is a fun, action-packed army builder with unique characters, challenges and engaging levels. Set forth on an adventure to protect friends, family and countrymen from new adversaries. Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.

      Previous Work by Team
      Although we are working towards our first game as a team, our team members themselves have past experience in the industry.
      This includes members who have worked on titles including:
      Final Fantasy Kingsglaive, FIFA, Xcom 2 and Civilization.

      Who are we looking for? 3D Modellers Concept Artists Marketing Specialists Level Designer

      What do we expect? Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer? Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      If you are interested in knowing more or joining. Please email or PM us on Skype. Myself or Colin will reply to you within 48 hours.

      Skype: ForgedInteractive

      David and Colin

      Follow us on:

      Twitter: @ForgedInteract
    • By Eck
      I just saw their courses were knocked down to $10 each and figured I'd share the info here. They have stuff for Unity, Unreal, drawing, business, etc. I haven't used their stuff before, but the previews I looked at seemed pretty good and there is a user review system as well.
      - Eck
    • By zizulot
      first and only logo , for now
  • Popular Now