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 gorgoroth · Nov 22, 2013 at 05:53 PM · getcomponentdamageguitextcommunicationhealth

Trouble with communication between scripts

I am trying to make an FPS survival game involving a single player and a number of cloned enemies. Each enemy has a simple AI that tells it to follow the player and when in a certain radius, it "attacks", involving a boolean that says whether or not the enemy is inflicting damage.

What I want is to have the player's health script decrease health when the damage boolean is true. I have not successfully accomplished this communication. Another script I created sets a GUIText to show the health variable and the damage boolean. This is what I have:

using UnityEngine; using System.Collections; public class HUD : MonoBehaviour { private AIEnemy aiStuff; private PlayerHealth playerhealths; public float theplayerhealth=0f; public bool damaging=false; void Start () { playerhealths = GameObject.Find("Player").GetComponent<PlayerHealth>(); aiStuff = GameObject.Find("TheEnemy(Clone)").GetComponent<AIEnemy>(); } // Update is called once per frame void Update () { theplayerhealth = playerhealths.HEALTH; damaging = aiStuff.DAMAGING; print (damaging); print(theplayerhealth); guiText.text = " " + theplayerhealth + damaging; } }

... where DAMAGING is the damage bool in the AI script, titled AIEnemy. The health script is PlayerHealth, and the health float is HEALTH.

When I run this script, nothing shows up and I get an error:
NullReferenceException

UnityEngine.GameObject.GetComponent[AIEnemy] ()

HUD.Start () (at Assets/Scripts/HUD.cs:14)

I really have no idea why I get this error or why nothing is working properly. If anyone can offer any help it would be greatly appreciated.

P.S. When I try to get the component from the enemy on line 14 (line 9 here), specifying TheEnemy or TheEnemy(Clone) does not appear to change anything. I have tried this with only one enemy spawned, and with multiple, neither situation appears to change anything.

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
1

Answer by gorgoroth · Nov 25, 2013 at 10:16 PM

Okay, I now realized that I was having this issue because I was referencing the AI script with void Start (); which ran only once and was running before the enemy had even spawned; at least that is my theory. By instead having a timer that waited until the enemy had been spawned before performing a one time function that defined the connection to the enemy AI script, I was able to make this work.

Thank you to smallbit for suggesting FindWithTag, that certainly helped.

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

Answer by smallbit · Nov 22, 2013 at 06:24 PM

Have you checked whether when referencing to this script, "TheEnemy(Clone)" exist? Try to rather find it using tag instead of name when working with instances. Have you double checked if the name is correct (typos)?

Comment
Add comment · Show 1 · 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 gorgoroth · Nov 22, 2013 at 07:01 PM 0
Share

I tried tagging the enemy prefab and using FindWithTag, but I still get an error: NullReferenceException: Object reference not set to an instance of an object HUD.Update () (at Assets/Scripts/HUD.cs:21).

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

17 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

Related Questions

hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great. 2 Answers

Adding fall damage with the default FPSWalker 1 Answer

This collision Enemy health code suddenly stopped working. 1 Answer

enemy attack not recognizing players stats 1 Answer

Making a Health Bar with Code 5 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