• 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 Bob1 · Sep 29, 2011 at 04:02 AM · nullreferenceexception

On Collision Enter Object Variable Acces NullReferenceException

I'm trying to access the stats of an opponent, but there is a nullreferenceexception for the lines defining optype1 and optype2

     function OnCollisionEnter(collision:Collision) 

{

 print("hi");

 var optype1 = collision.gameObject.GetComponent(BaseStats).type1;

 var optype2 = collision.gameObject.GetComponent(BaseStats).type2;

How should I fix it?

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

Answer by aldonaletto · Sep 29, 2011 at 04:10 AM

You may be having collisions with the ground or other objects that don't have the script BaseStats. The easiest way to avoid this is to check if the object hit has a BaseStats script:

function OnCollisionEnter(collision:Collision) 
{
  var baseStScript: BaseStats = collision.gameObject.GetComponent(BaseStats);
  if (baseStScript){
    var optype1 = baseStScript.type1;
    var optype2 = baseStScript.type2;
    ...
  }
}
Comment
Add comment · Show 3 · 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 Bob1 · Sep 29, 2011 at 04:47 PM 0
Share

I've tried it and it helps, but there is another error in the last line of the following:

var baseStScript: BaseStats = collision.gameObject.GetComponent(BaseStats);

   if (baseStScript)

 {

 print("hi");

 var optype1 = baseStScript.type1;

 var optype2 = baseStScript.type2;

 var STAB = shooter.GetComponent(BaseStats).type1;

 var STAB2 = shooter.GetComponent(BaseStats).type2;



 if(movestyle==0)

 {    

     def = baseStScript.defense;

     atk = shooter.GetComponent(BaseStats).attack;

 }

 if(movestyle==1)

 {

     def = baseStScript.specialdefense;

     atk = shooter.GetComponent(BaseStats).specialattack;

 }





 damage = $$anonymous$$athf.RoundToInt(((((2*shooter.level/5+2)*atk*basepower/def)/50)+2)*STAB*STAB2*(Random.Range(85,100)/100));

It's saying that: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) DamageScript.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Standard Assets/Reusable Scripts/ New Scripts/Generic/DamageScript.js:36)

avatar image aldonaletto · Sep 29, 2011 at 07:02 PM 0
Share

Which line is 36? The last one? In the last line you have shooter.level, which doesn't seem to make much sense: from the other lines, shooter seems to be a Transform of GameObject, and none of them have a level property. Could it be a BaseStats variable? By the way, it would be better to save at the beginning a reference to the shooter BaseStats script, like we did with the variable baseStScript:

var shooterScript: BaseStats = shooter.GetComponent(BaseStats);
var STAB = shooterScript.type1;
var STAB2 = shooterScript.type2;
...
avatar image Bob1 · Sep 29, 2011 at 09:11 PM 0
Share

shooter is a transform/gameobject variable that yet another script acesses and sets. It's not assigned by BaseStats.

A move script on the shooter does this:

instanceball.GetComponent(DamageScript).shooter = transform;

then the bullet has the damage script with the variable shooter

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Instantinate NullReferenceError problem 0 Answers

Trouble Instantiating Projectiles 1 Answer

Why am I getting this NullReferenceException error when everything seems to be in place ? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 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