Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Pedro Johnston · Jan 15, 2014 at 03:04 AM · instantiateparticlesnullreferenceexceptionexplosion

Particles not Instantiating : NullReferenceException

Ok, so below is the script that is giving me trouble. I'm pretty sure i've followed the instructions given on the "Instantiate" reference, but i keep getting a "NullReferenceException: Object reference not set to an instance of an object" error, on the line where i give the Instantiate instruction.

What object do i need to instantiate? is it not properly instantiated?

Thanks ahead for any help, it will come in very valuable.

 public var maxHealth = 100;
 var currentHealth = maxHealth;
 public var explosionObj : GameObject;
 
 function Start(){
 
 
 
 }
 
 
 public function Damage (dmg : int){
     currentHealth = currentHealth - dmg;
 
 
 
     if (currentHealth == 100){
     
     }
 
     if (currentHealth >= 75 && currentHealth < 99){
     
     }
 
     if (currentHealth >= 50 && currentHealth < 74){
     
     }
 
     if (currentHealth >= 25 && currentHealth < 49){
     
     }
 
     if (currentHealth > 0 && currentHealth < 24){
     
     }
 
     if (currentHealth < 0){
 
         if (gameObject.name == "Player_0")
             Application.LoadLevel("teste leve");
             
         else{
             Debug.Log("i should be dead");
             var explosion : GameObject = Instantiate(explosionObj, gameObject.position, gameObject.rotation);
             Destroy(gameObject);    
         }
     }
 }
 
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 dkfister · Jan 15, 2014 at 03:09 AM 0
Share

Are you sure, that the variable "explosionObj" actually has the "Explosion Prefab" in it's correct slot, in the inspector?

avatar image Pedro Johnston · Jan 15, 2014 at 03:15 AM 0
Share

I am positive, however, what i did was to create a Particle System an then put it into a prefab. I then put this prefab in the inspector slot. Is this wrong?

avatar image dkfister · Jan 15, 2014 at 03:20 AM 0
Share

That is absolutely NOT wrong :) A particle system, is a GameObject, just like anything else in your scene.

avatar image Pedro Johnston · Jan 15, 2014 at 03:23 AM 0
Share

Something else funny happens. $$anonymous$$y "hero" shoots bullets at the character where this script is attached. When the "currentHealth" mentioned above goes below zero, the bullets no longer disappear, as they did until that point, and just weirdly bounce off him.

avatar image sanmn19 · Jan 15, 2014 at 03:29 AM 0
Share

Is this script attached to the enemy? If so, to make the bullet disappear you need to attach a script to bullet to make it destroy itself on collision with enemy. However, I do not know the solution to your original problem. Did you do these steps?

  1. Create a particle system. Tweak it.

  2. Create new prefab.

  3. Drag and drop the create particle system object to the prefab.

  4. Drag and drop the prefab to the script linked to the gameObject in the scene.

Did you follow these steps exactly?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by dkfister · Jan 15, 2014 at 03:49 AM

I'm pretty sure, gameObject doesn't have a position and a rotation alone by itself. These variables, come from the component ON the gameObject, which is transform.

So you have to change this line:

 var explosion : GameObject = Instantiate(explosionObj, gameObject.position, gameObject.rotation);

To:

 var explosion : GameObject = Instantiate(explosionObj, transform.position, transform.rotation);
Comment
Add comment · Show 1 · 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 Pedro Johnston · Jan 15, 2014 at 01:55 PM 0
Share

Thank you, man, you're awesome! It worked. Not quite sure why i used gameObject, i guess i didn't give it much thought! I can't up vote your answer because my rep is below 15, but thank you so much.

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

20 People are following this question.

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

Related Questions

NullReference on trying to instantiate a prefab which has been set using scriptable object,Particle giving null reference but works in scene view 0 Answers

bind instantiated particles to an object 1 Answer

A question related to the explosion after creation of debris 1 Answer

Instantiate giving a NullReferenceError 1 Answer

Null Reference when trying to instantiate a prefab not in the scene 0 Answers


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