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 Diopap · Apr 19, 2012 at 10:03 AM · referencenullexception

Null Reference exception error

Hey guys,i recently started using unity for a game project that i have been planning.I have been following the tornado twins tutorials in youtube to learn most of the enviroment , i encountered the Null Reference exception problem and i really have no idea how to fix it since i don't know javascript that well.

The script basically makes a turret from firing bursts of many fireballs down to one fireball every two seconds,though even after i did that the video said i get loads of fireballs instead of one,here is the script.The errors that i get are from the compiler are from the shoot function,var bullit and the line below.Can anyone lend me a hand?

 var LookAtTarget:Transform;
 
 var damp = 6.0;
 
 var bullitPrefab:Transform;
 var savedTime=0;
 var spawnPoint:Transform;
 
 function Update ()
  {
 
    if(LookAtTarget)
     {
  var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
 
 
 transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
 
 
   var seconds : int = Time.time;
   var oddeven = (seconds % 2);
   
   
     if(oddeven)
          { 
       
       
          Shoot(seconds);
     
     
          }
     
     }
 
 
 }
 
 
 
 
 function Shoot(seconds) 
 
 {
    if(seconds!=savedTime)
 
           {
        var bullit = Instantiate( bullitPrefab, transform.Find("spawnPoint").transform.position , Quaternion.identity);
 
 
          bullit.rigidbody.AddForce(transform.foward * 1000);
   
          savedTime=seconds;
      }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Apr 19, 2012 at 11:05 AM

Mmm bullit.rigidbody.AddForce(transform.foward * 1000); "forward" can you check if that goes better?

Comment
Add comment · Show 6 · 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 Diopap · Apr 19, 2012 at 03:19 PM 0
Share

But the bullet Prefab is already in the inspector,i simply added the item that it should throw,in this case a fireball..

avatar image fafase Diopap · Apr 19, 2012 at 03:29 PM 0
Share

Did you change what I told you about the forward. You said "The errors that i get are from the compiler are from the shoot function,var bullit and the line below" and right below is this misspelling of forward.

avatar image Diopap Diopap · Apr 19, 2012 at 07:36 PM 0
Share

It was exactly that!!It fires as it should now,once every 3 seconds!Though i still get a null ref exception at the 46th line,right above.

avatar image Diopap · Apr 19, 2012 at 07:35 PM 0
Share

Yup that fixed the problem in that line!!!It finally shoots one bullet every 3 seconds!!!I still get like 50 errors at the 46th line (the one directly above),though it appears not to cause any trouble..should i be worried?

avatar image fafase · Apr 20, 2012 at 09:33 AM 0
Share

Warning can be ignored, errors are errors and might get back to you at some point. Try to isolate the origin. Try to modified the parameters of Instantiate one by one and see, if for example you use Vector3(0,0,0) ins$$anonymous$$d of the spawnPoint transform and the errors disappear, then you found it.

But, one clue could be that you error is co$$anonymous$$g in the update since you get it many times a second and shoot() is only accessed once every 2 sec. It could be that seconds or oddeven are doing wrong somehow.

At the moment, I cannot find anything wrong...

avatar image Diopap · Apr 20, 2012 at 10:59 AM 0
Share

I'll look more into it,thanks for helping me out again!I started using Unity like five days ago and everything is still fairly new to me.

avatar image
0

Answer by Wolfram · Apr 20, 2012 at 11:16 AM

Shoot() is called several times per second, since you cast Time.time to an integer, so for any time between 1.0000 and 1.9999, seconds will be 1, and Shoot() will be called. Normally, your usage of "savedTime" would prevent several bullets being spawned, but if you get a NullReferenceException, any code after that error is never executed, so "savedTime" is never updated.

Note that Find("spawnPoint") will search for an object named "spawnPoint" in your scene hierarchy - it has nothing to do with your variable "spawnPoint". So make sure you have a GameObject named "spawnPoint" in your scene. Or remove the transform.Find("spawnPoint").transform.position and simply replace it with spawnPoint.position, which will refer to your variable.

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

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

6 People are following this question.

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

Related Questions

NullReferenceException Error 6 Answers

Instantiate() as GameObject = null reference 1 Answer

Why is this code getting NullReferenceException 2 Answers

Null Reference Exception error 3 Answers

NullReferenceException - With Network PlayerSpawn 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