Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Arju2011 · Nov 25, 2015 at 07:33 AM · javascriptgameobjectnullreferenceexceptiongetcomponent

Detecting for a variable from multiple potential scripts.

I have multiple objects, that deal damage with the colliders, although I get a lot of errors and null stuff.

Original code was simply:

     ////Grab damage of collision
     damage = other.gameObject.GetComponent("Z95Projectile").damage;
         damage = other.gameObject.GetComponent("MediumExplosion").damage;
             damage = other.gameObject.GetComponent("SmallExplosion").damage;

Failed attempted solution:

 ////Grab damage of collision
         damage = other.gameObject.GetComponent("Z95Projectile").damage;
         if (damage == null)
         {
         damage = other.gameObject.GetComponent("MediumExplosion").damage;
             if (damage == null)
                 {
                 damage = other.gameObject.GetComponent("SmallExplosion").damage;
     
                     if (damage  == null) {
                     //later
                     
     }    }    }    
 

This answer sort of helped, but not completely, because i am trying to use multiple different scripts, not players. http://answers.unity3d.com/questions/644841/nullreferenceexception-object-reference-not-set-to-83.html

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

Answer by fafase · Nov 25, 2015 at 08:00 AM

The issue there is that you are using GetComponent with a string. This results a reference of type Component. That component reference still needs to be cast down tot he type containing the variables.

I would recommend looking into interfaces. In your case, you would declare a IDamageable interface with a Damage property.

 public interface IDamageable {
      int Damage { get ; set;}
 }

Then all three scripts you are dealing with would implement the interface and you can use one line then.

 IDamageable damageable = other.gameObject.GetComponent<IDamageable>();
 if(damageable != null){
      int damage = damageable.Damage;
 }
Comment
Add comment · Show 7 · 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 Arju2011 · Nov 25, 2015 at 08:20 AM 0
Share

I'm having trouble finding documentation for this.

avatar image Torigas Arju2011 · Nov 25, 2015 at 08:36 AM 0
Share

This is just basic software design. Take a moment studying c# and interfaces. You should specify the errors when you look for an answer. "A lot of errors and null stuff" doesn't tell us much. Also you should try looking up said errors first. Just google them. Your case is quite a classic example of a problem where a debugger would help. If you get a null pointer you have to find out where the null pointer is. Which variable is null. As fafase so nicely explained, the GetComponent call returns a null pointer when the component is not available. Since you are not checking it for null but directly accessing a variable, the thing will throw an exception. https://unity3d.com/learn/tutorials/modules/beginner/scripting/monodevelops-debugger

http://docs.unity3d.com/ScriptReference/Component.GetComponent.html if you read the reference you'll see it explicitly says that you get a null reference when the component does not exist.

avatar image Arju2011 Torigas · Nov 25, 2015 at 08:39 AM 0
Share

I linked to a different question with the same error... :\ http://answers.unity3d.com/questions/644841/nullreferenceexception-object-reference-not-set-to-83.html

avatar image SummitJain · Nov 25, 2015 at 09:24 AM 0
Share

The scripts that count damage are multiples and damage is the variable which is incremented on collision. So the way you have written script where declaring same variable name for multiple scripts makes it complicated to debug and leads to issues later on if declared at multiple places like getComponent script.

You should create only one script as a manager. Score$$anonymous$$anager and declare the required variables and use singleton pattern for the instance of the script and call it directly in other scripts rather than as a GetComponent.vraiable. ".

If you want to check where damage in all scripts where it is getting referenced and called. You can go to the script and right click on variable and click on go to declaration and click on find references. It will show you a list where the variable is called. You can enter a debug on collision.

Always remember to use some design pattern for scripts if you calling it from other scripts. for better debugging, execution and optimization.

using UnityEngine;

public class HUD$$anonymous$$anager : $$anonymous$$onoBehaviour { public static Score$$anonymous$$anager sInstance; public static int damage; void Awake() { sInstance= this; }

void Damage() { Score$$anonymous$$anager.sInstance.damage++; } } //call damage wherever script you want by "Score$$anonymous$$anager.sInstance.damage++;"

avatar image Arju2011 SummitJain · Nov 25, 2015 at 11:20 AM 0
Share

What I have working at the moment is : if (other.name == "thisname" ) { Damage = 7;}

Im not sure if this method will have negative performance later on. But its better than the code in the question.

avatar image NoseKills · Nov 25, 2015 at 03:49 PM 0
Share

Does GetComponent nowadays work with interfaces? It used not to... Only normal inheritance & base classes

avatar image fafase NoseKills · Nov 26, 2015 at 05:14 AM 0
Share

It actually does from Unity5. It used to be that you had to cast and so on but no more. You still can't use it in the inspector since it is no $$anonymous$$onoBehaviour and cannot be serialised.

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

43 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

Related Questions

GetComponent is not a member of Object 0 Answers

How to rotate one cube from another cube? 1 Answer

Store script from GetComponent ? 1 Answer

Why does FindObjectOfType().transform return a null reference 1 Answer

NullReferenceException on second line but not first? 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