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
1
Question by lampshade · May 27, 2010 at 02:13 AM · nullreferenceexceptioncs0266

Instantiate particle effect for the player

Hi,

What I have done so far is created a "particle system" under GameObject->Create Other and added it to my scene. I can now see 1 group of particles emitting.

So we want to create a new folder called "Resources" under assets in my project directory and within it, create an empty prefab. We can call this empty prefab, "ParticlePrefab".

I go to my heirarchy select the Particle System (default naming convention) and drag it to my ParticlePrefab, which changes it from a whitish color, to a bluish color.

I go to my player C# script, add the statement:

public GameObject ParticlePrefab;

I go to my player (game object) and drag the ParticlePrefab onto the ParticlePrefab slot, so that my Player is aware of the particle system.

So now I want to Instantiate the ParticlePrefab when I press a key, But I can already see it happening off to the side in the scene.

Maybe, with this code someone can help see why: using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

public GameObject ParticlePrefab;

void Start() { // Intialize game start code }

void Update() { // Move Player code }

void OnTriggerStay(Collider other) {
if (Input.GetKeyDown("h"))
{ Instantiate(ParticlePrefab); } // ... } }

I am not getting a Null reference error for the prefab, "ParticlePrefab"

Comment
Add comment
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

1 Reply

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

Answer by Mike 3 · May 27, 2010 at 02:38 AM

Is the one from the scene (which you made the prefab from) still there?

If so, remove it

Edit:

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

public GameObject ParticlePrefab; private GameObject instantiated; private Vector3 lastPos;

void Start() { // Intialize game start code }

void Update() { // Move Player code if(Vector3.Distance(lastPos, transform.position) > 0.01f) { lastPos = transform.position; if (instantiated != null) Destroy(instantiated); } }

void OnTriggerStay(Collider other) {
if (Input.GetKeyDown("h")) //one at a time { instantiated = (GameObject)Instantiate(ParticlePrefab, transform.position, transform.rotation); } // ... } }

Comment
Add comment · Show 16 · 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 lampshade · May 27, 2010 at 02:55 AM 0
Share

Alrighty, so I think that what you suggested works. However, what I am going for is that the player needs to be able to emit particles from its center of mass when the user presses "h"

avatar image Mike 3 · May 27, 2010 at 02:58 AM 0
Share

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

avatar image lampshade · May 27, 2010 at 03:04 AM 0
Share

How to I destroy the ParticlePreFab? Destroy(ParticlePrefab) gives me errors.

avatar image Mike 3 · May 27, 2010 at 03:07 AM 0
Share

You need to store a reference to it when you create it, will edit my answer to reflect that

avatar image lampshade · May 27, 2010 at 03:19 AM 0
Share

I can't see that working ;x

Show more comments

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

No one has followed this question yet.

Related Questions

Getting a button to run a function 2 Answers

WWW.CheckSecurityOnHeaders throwing NullReferenceException 1 Answer

NullReferenceException? 2 Answers

Null Reference Expantion 1 Answer

Null Reference Exception For Blocks C# 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