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 EmpI · Nov 21, 2014 at 07:34 AM · bulletsobject-reference-error

Confusing Object Reference Error

So I have a script which spawns a bullet. Nothing unusual, I have scripts that spawn bullets.

Except for some reason, whenever I try to get it to work, it tells me the object reference is not set to an instance of an object, but I can't seem to figure out why.

Here is what the code looks like

     void Update () {
         timer = timer + 1;
 
         if (timer == 90){
             spawnbullet = A1;
             Dir = ShootDown;
             Fire();
         }
         if (timer == 180){
             spawnbullet = A2;
             Dir = ShootLeft;
             Fire();
         }
         if (timer == 270){
             spawnbullet = A3;
             Dir = ShootRight;
             Fire();
         }
     }
 
     void Fire(){
         Rigidbody2D Shot;
         Shot = Instantiate (projectile, spawnbullet.position, Quaternion.Euler (Dir)) as Rigidbody2D; //Dir and the above ShootLeft/ShootRight are Vector3 variables
         Shot.velocity = transform.TransformDirection (Vector3.forward * 5); //The error returns here.
         Shot.transform.Rotate (Vector3.forward); //A similar error appears when I comment out the above line.
     }

The code works fine if I comment out the Shot.velocity and Shot.transform.Rotate lines the script works fine. At the 90, 180, and 270 frame, bullets spawn at A1, A2, and A3 respectively, however they just hover in space, which won't do at all for the game i'm trying to build.

So why is it returning an error, is there an instance or some information the game is missing? The Assembly isn't returning any errors and i'm terribly confused, since I use this exact code to spawn projectiles from other enemies in the game, all of which are fully functional and operate without issue.

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
0
Best Answer

Answer by HarshadK · Nov 21, 2014 at 07:38 AM

You are instantiating your Shot as a rigidbody. Rigidbodies don't have transform component that is why you get that error.

What you should rather do is instantiate shot as game object and add a rigidbody component to it as below:

 GameObject Shot;
 Shot = Instantiate (projectile, spawnbullet.position, Quaternion.Euler (Dir)) as GameObject; //Dir and the above ShootLeft/ShootRight are Vector3 variables
 Shot.AddComponent<Rigidbody2D>();

Now you can perform transform operations on your Shot game object as previous.

Comment
Add comment · Show 4 · 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 EmpI · Nov 21, 2014 at 07:44 AM 0
Share

The assembly returns an error, as GameObject doesn't contain a definition for "velocity" which is what I need.

avatar image HarshadK · Nov 21, 2014 at 07:47 AM 0
Share

It is because velocity is a property of your rigidbody and not game object. Get a reference to your new Rigidbody2D component you added and then set velocity for it like:

 Rigidbody2D shotRigidbody = Shot.AddComponent();
 shotRigidbody.velocity = transform.TransformDirection (Vector3.forward * 5);

avatar image EmpI · Nov 21, 2014 at 08:54 AM 0
Share

making it shot.rigidbody2d.velocity fixed it. However for some reason, the shots still hung there for a while. After a bit of playing around, replacing Vector3.forward with the variable Dir got the projectiles to move as I like them to.

Problem solved.

avatar image Baste · Nov 21, 2014 at 09:55 AM 0
Share

Please accept the question if it solves your problem, as otherwise it hangs out as "unanswered".

Also, variables should not be capitalized. So your variables Dir, Shot, ShootDown, and so on should be 'dir', 'shot' and 'shootDown'. This is to differentiate them from classes and methods, which should be capitalized.

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

2 People are following this question.

avatar image avatar image

Related Questions

Health System Raycast Bullets 2 Answers

Bullets fall 4 Answers

Bullet Hole sprites don't align correctly on walls 2 Answers

How do I destroy a gun bullet clone? 3 Answers

Ai can fire Infinite bullets? 2 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