x


How do I use CreateInstance with a non-default constructor in C#

How can I use CreateInstance with custom constructors:

using UnityEngine;
using System.Collections;

public class Example : ScriptableObject
{
  // don't allow a call to the default constructor
  private Example () {}

  // how do I use CreateInstance to create an instance of 
  // Example that calls this constructor
  public Example (string message)
  {
    Debug.Log ("It worked!! " + message);
  }
} 

Are there any examples of this anywhere?

more ▼

asked Dec 14 '10 at 02:48 PM

AudiOishi gravatar image

AudiOishi
72 7 8 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can't, but you can add in an init function which you can call after CreateInstance (and can be called from the constructor(s) with the parameters to keep the code sane)

more ▼

answered Dec 14 '10 at 03:57 PM

Mike 3 gravatar image

Mike 3
30.7k 10 67 255

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x143
x74
x40

asked: Dec 14 '10 at 02:48 PM

Seen: 1895 times

Last Updated: Dec 14 '10 at 02:48 PM