Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Isa · Sep 06, 2011 at 12:26 PM · instantiateprefabnullreferenceexceptioncompiler

NullReferenceException: Object reference not set to an instance of an object

Hello, i've got this compiler message but don't know how to solve it. It's about this part of my js code:

 for(stein in steineliste){
     stein.CreateStone();
 }

stein is an object of class Stone which has the function CreateStone().

With this code used it works fine:

 function CreateStone(){
     stone = GameObject.CreatePrimitive(PrimitiveType.Cube);
     stone.transform.position = Vector3(pos_X, pos_Y,0);
     stone.transform.localScale += Vector3(width,height,0);
 }

But as soon as I use my Prefab I get the compiler message mentioned in the title:

 function CreateStone(){    
     stone = Object.Instantiate(UnityEngine.Resources.Load("CubePrefab"),Object.transform.position,Object.transform.rotation);
     stone.transform.position = Vector3(pos_X, pos_Y,0);
     stone.transform.localScale += Vector3(width,height,0);

 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPedersen · Sep 06, 2011 at 12:47 PM 0
Share

First off, the message you're getting isn't from the compiler. Exceptions are run-time errors, they're thrown when errors occur after your program has passed compilation.

Second, a NullReferenceException happens when you're trying to use some object in your code that doesn't point to anything, i.e. it refers to null, a "NullReference". In this case, it's probably because Resources.Load failed to locate an asset with the name CubePrefab.

Object.transform.position doesn't look right either... Object doesn't have a member called "transform". That code shouldn't pass compilation.

avatar image Isa · Sep 06, 2011 at 01:02 PM 0
Share

Thx, I changed the Object.transform things to transform.position/rotation: BCE0005: $$anonymous$$ identifier: 'transform'. And Assets/Resources contains a CubePrefab.prefab. How can i check if it's loaded?

avatar image CHPedersen · Sep 06, 2011 at 01:10 PM 0
Share

Follow aldonaletto's instructions below, and create a public Transform to hold the prefab, then drag it onto that in the editor.

avatar image Isa · Sep 06, 2011 at 02:37 PM 0
Share

Doesn't solve the problem :( $$anonymous$$aybe I am the problem :(

avatar image aldonaletto · Sep 07, 2011 at 12:36 AM 0
Share

Take a look at my answer: I removed the first line of CreateStone, because it was creating a temporary prefab variable - and you wanted to use the public prefab.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Sep 06, 2011 at 12:55 PM

The problem is that you're using the Object class instead of an actual instance to define position and rotation. I would also store the "CubePrefab" in a GameObject variable to define its type, like this:

function CreateStone(){
    var prefab: GameObject = Resources.Load("CubePrefab"); // define the type as GameObject
    var stone = Instantiate(prefab, Vector3(pos_X, pos_Y,0), transform.rotation); 
    stone.transform.localScale += Vector3(width,height,0);
}

EDITED: If you created a public variable prefab outside the function to drag "CubePrefab" to, just remove the first line of CreateStone, like below:

public var prefab: GameObject; // <- drag your prefab here

function CreateStone(){ var stone = Instantiate(prefab, Vector3(pos_X, pos_Y,0), transform.rotation); stone.transform.localScale += Vector3(width,height,0); }

Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Isa · Sep 06, 2011 at 01:29 PM 0
Share

If i use this code, i get following warnings: Ambigous reference Resources $$anonymous$$ identifier: Instantiate $$anonymous$$ identifier: transform.

I even tried something like var prefab = "Resources/CubePrefab"; stone = Instantiate(prefab,...,...); Same problem again :(

$$anonymous$$aybe it's the problem that the code is written in a class which can't inherit from GameObject as it isn't possible to do something like class Stone extends GameObject?

avatar image Isa · Sep 06, 2011 at 02:04 PM 0
Share

ok, got it. Almost. i restarted unity and now i can drag the Prefab in the inspector to the public variable i created. BUT: If I do this, he doesn't know the identifier prefab in the CreateStone-function. Even if the public var prefab : GameObject has the same name. And if i create the public variable in the function as you did it in the example, i can't drag the Prefab in the instructor to this variable. Or if i copy the Resource.Load part, too, I still get the Warnings mentioned the answer before...

avatar image aldonaletto · Sep 07, 2011 at 12:34 AM 0
Share

That's because you're still creating a prefab temp variable inside your function. Take a look at my answer above: I edited it to include this case.

avatar image Isa · Sep 07, 2011 at 10:16 AM 0
Share

Thx for your help again. I did exactly what you told me to do but it doesn't work: In the console I'm told that Instantiate is an unknown identifier and transform.rotation, too. If i add an Object. in front of Instantiate and transform, I still get this NullReferenceException. It seems to be right what you're telling me, that's not the point. But why doesn't it work? $$anonymous$$aybe because I do the Instantiate in a different class? The file has a function awake, a function which reads a xml file and two classes, one extends the other class. And I'm doing the Instantiate in the Subclass...

avatar image StephanK · Sep 07, 2011 at 10:58 AM 0
Share

Im not sure about js as I use C#, but afaik you are not supposed to write js classes. If you do so you have to make sure your classes are derived from $$anonymous$$onoBehaviour otherwise you can't access things like transform or Instantiate. Also it's probably not possible or at least bad practice to have multiple classes in the same file with unity as unity requirese all components to be in a file that has the same name as the actual class. For JS you don't even need a class it will create that for you from the file.

Show more comments
avatar image
0

Answer by StephanK · Sep 06, 2011 at 12:45 PM

Probably it can't find the prefab in the resources folder. Try declaring a public variable, assign your prefab to that variable from the inspector and use this instead of Resources.Load.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Isa · Sep 06, 2011 at 01:13 PM 0
Share

I tried this: public var prefab;

but can't assign the prefab to this variable in the inspector. What am I doing wrong?

avatar image StephanK · Sep 06, 2011 at 01:16 PM 0
Share

I guess you have to tell unity what the type of the variable is. So try public var prefab : GameObject;

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variable Assigning with GameObject.Find("") causing NullReferenceException? 0 Answers

Accessing a prefab after instantiating results as null 1 Answer

initial velocity for instantiated ragdoll prefab 1 Answer

Help how to reference prefab for instantiating between loads. 0 Answers

instantiate prefab object to existing object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges