Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by TheyLeftMe4Dead · Jan 16, 2017 at 03:01 AM · instantiatereferenceparametersnull reference exception

NullReferenceException problems.

Hello, Unity Answers. I'm having trouble with a NullReferenceException. The error reads : Object reference not set to an instance of an object. I know what this means. What I don't know is why/how I am getting the error. I've done some further research on my own but I haven't found anything practical to my issue (I'm working on a Tower Defense game if that helps).
My code is below:

 #pragma strict
 
 //BulletHit will move towards an enemy. If it hits the enemy,
 //the enemy will recieve the bullets damage.
 //Declaring variables ::
 var bulletDmg : int;
 var bulletSpeed : float = 1;
 var despawnTime : float = 2;
 private var bulletRB : Rigidbody2D;
 
 //Runs on start.
 function Start ()
 {
     bulletRB = GetComponent.<Rigidbody2D>();
     Despawn();
 }
 
 //Adds a force to the bullet in the direction of the enemy.
 function MoveToEnemy (towerPos : Vector3, enemyPos : Vector3) {
     //forceDir is equal to the Vector3 between the enemy & tower
     var forceDir : Vector3 = GetEnemyDir(towerPos, enemyPos);
     bulletRB.AddForce(forceDir * bulletSpeed);          //ERROR HERE
 }

Note: towerPos and enemyPos are given to MoveToEnemy from the class BasicTower. I've debugged BasicTower to insure that the sent items are not null.
function GetEnemyDir:
Returns a given enemy Vector3 converted to coordinates relative to the tower.

Comment
Add comment · Show 1
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 JedBeryll · Jan 16, 2017 at 06:25 AM 0
Share

Which line does the error point to? I'm a C# dev but to my best knowledge this code: bulletRB = GetComponent.<Rigidbody2D>(); should not work in JS (I could be wrong tho). It should be something like GetComponent(RigidBody) as RigidBody.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Call function for instantated prefab. "Object reference not set" 1 Answer

Is it overkill to RequireComponent then check if GetComponent isnt Null in Awake? 1 Answer

Getting error NullReferenceException on instantiate 3 Answers

Instantiate leading to reference not set error 0 Answers

Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object. 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