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 AMDAndrew · Oct 25, 2012 at 06:02 PM · variablesstaticenemyaiglobal

Not asigned health script

Hi, I am having a small problem which I don't understand.

I am importing health script corectly in my EnemyAI but when it's about taking damage I get the error that the component is not asigned.

     public HealthBarGUI health;
     
     void Awake(){
     
         myTransform = transform;
         controller = GetComponent<CharacterController>();
         health = GetComponent<HealthBarGUI>();
         currentWaypoint = WayPoint1;
 .
 .
 .
 .
 .
     void Attack (){
         
     if( attackDelay == 0 )
             attackDelay ++;
         animation.CrossFade(attackAnimation.name);
         health.curHealth = health.curHealth - maxDamage;
     
     if( attackDelay    > 3)
             attackDelay = 0;
         
     }
     }

And the error I get is the following :

"Object reference not set to an instance of an object on line health.curHealth" EnemyAIC.Attack

Comment
Add comment · Show 6
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 Yokimato · Oct 25, 2012 at 06:58 PM 0
Share

on your start and attack, can you do a simple Debug.Log(health == null); see if it finds it initially, and that it doesn't somehow get removed before attack happens? If the check fails on the awake function, you dont have the script on the gameobject. and if the attack check fails, it's getting removed from somewhere.

avatar image phodges · Oct 25, 2012 at 07:07 PM 0
Share

Another simple thing to check is there is actually a HealthBarGUI on the gameobject in the first place. Calling GetComponent correctly will still return null if there isn't one there to be found.

avatar image AMDAndrew · Oct 25, 2012 at 07:22 PM 0
Share

the debug log showed me : "Error you don't want" in start for health == null Altrough I don't know why because I have the script attached to my main character :-?

avatar image phodges · Oct 25, 2012 at 07:28 PM 0
Share

Didn't you say you were attaching this script to enemies too? GetComponent is not searching your whole scene for the GUI health component, just the current object.

avatar image AMDAndrew · Oct 25, 2012 at 07:47 PM 0
Share

In the inspector appears the object containting that script like

Health $$anonymous$$AX(HealthBarGUI)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AMDAndrew · Oct 25, 2012 at 09:27 PM

I just found out that without this declaration

 health = GetComponent<HealthBarGUI>();

it will work just fine but you have to attach the script by yourself.

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

11 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

Related Questions

Static variable issue in published 1 Answer

My static variable is not changing 2 Answers

Global Variables Refuse to Cooperate 1 Answer

Changing static variables from another script? 1 Answer

Global Varible Problem 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