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 Danzo1 · Apr 28, 2014 at 01:01 PM · nullreferenceexceptionprojectiles

Trouble Instantiating Projectiles

Attempting to create a top down shooter where the player can shoot zombies that spawn. I'm having trouble getting the projectiles to work, however. I'm new to unity so bear with me.

public Rigidbody projectile;

if (Input.GetMouseButtonDown(0)) { if (bulletCount > 0) { anim.Play ("Strike");

             ammo -= 1;
             Rigidbody bullet = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
             Physics.IgnoreCollision(bullet.collider, collider);
             bullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));


         }
     }

This is the player's function that creates the bullet.

public class bullet : MonoBehaviour {

 public void OnTriggerEnter(Collider c) {
     Debug.Log("zombie died");
     GameObject.FindGameObjectWithTag("Player").GetComponent<Tom>().decreaseMobCount();
     Destroy(c.gameObject);
     Destroy(this.gameObject);


 }

}

This is the bullet's function. Whenever I click in game, it tells me there is a nullreference exception, object reference not set to an instance of an object. I don't understand the issue here.

Comment
Add comment · Show 10
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 KillMobil · Apr 28, 2014 at 01:07 PM 0
Share

So does the error come from the above scripts?

also i imagine that you would want OnCollisionEnter rather OnTriggerEnter

avatar image Danzo1 · Apr 28, 2014 at 01:12 PM 0
Share

There must be a problem with the way Im instantiating, I keep getting that same error.

avatar image KillMobil · Apr 28, 2014 at 01:30 PM 0
Share

wich line do you get the error? cant you see?

avatar image Danzo1 · Apr 28, 2014 at 01:39 PM 0
Share

I get the error on both lines right under creation of the bullet.

avatar image KillMobil · Apr 28, 2014 at 01:52 PM 0
Share

ive just tried the script my self and it worked... I Guess check that your Projectile you choose to duplicate has already a RigidBody and a collider.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by screenname_taken · Apr 28, 2014 at 01:37 PM

Shouldn't you spawn a gameobject that has a rigidbody attached, and then apply the physics to that rigidbody? You are spawning a rigidbody directly i think. (Not familiar with C# yet.)

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 KillMobil · Apr 28, 2014 at 01:50 PM 0
Share

well that what that suppose to do, but i just means that "bullet" is referencing the rigibody of the new GameObject.

avatar image screenname_taken · Apr 28, 2014 at 06:20 PM 0
Share

I thought that it was making a wild rigidbody out in the open. That's why it wasn't finding any colliders.

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

22 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 avatar image avatar image

Related Questions

NullReferenceException 2 Answers

Material[] Object reference not set when instantiating 2 Answers

NullReference Exception when adding a class 2 Answers

NullReferenceException 1 Answer

Raycasthit NullRException 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