x


Calling Instantiate from a function within a class

Trying to call Instantiate from within a function in a class. Here's what it looks like:

class Marker {
   function Action() {
      Object.Instantiate(Resources.Load("t_obj"), Vector3(10.0, 20.0, 100.0), Component.Transform.rotation);
   }
}

The error I get is: "NullReferenceException: Object reference not set to an instance of an object."

If I call Instantiate outside of the class (as part of an OnMouseDown function in another file) it works fine.

more ▼

asked Aug 10 '11 at 07:21 PM

grayfox gravatar image

grayfox
71 7 9 11

Fixed my problem. The correct Instantiate line should read:

UnityEngine.Object.Instantiate(Resources.Load("t_obj"), Vector3(10.0, 20.0, 100.0), Quaternion.identity);

Working as it should now.

Aug 11 '11 at 02:18 PM grayfox
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Component.Transform.rotation isn't a real thing. Perhaps you mean transform.rotation?

more ▼

answered Aug 10 '11 at 09:56 PM

sneftel gravatar image

sneftel
1.7k 7 9 20

Without 'Component' I get the error: "Unknown identifier: 'transform'."

Aug 11 '11 at 01:58 PM grayfox
(comments are locked)
10|3000 characters needed characters left

You don't need to use 'Object' as a prefix. IE...

Instantiate (prefab, transform.position, transform.rotation);

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

more ▼

answered Aug 11 '11 at 10:59 AM

AaronG gravatar image

AaronG
261 16 18 22

It's okay to do so. That's not the source of his error.

Aug 11 '11 at 12:31 PM sneftel
(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:

x1682
x337
x218

asked: Aug 10 '11 at 07:21 PM

Seen: 912 times

Last Updated: Aug 11 '11 at 10:14 PM