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 /
avatar image
0
Question by stewie_7999 · May 05, 2019 at 10:04 AM · scripting problemprefabcanvas

Finding Player Health using script for Enemy Prefab

Hi,

So I'm creating a top down 2D roguelike and I want to spawn enemies in each room. However since this is to be randomized multiple of the same enemy types can exist at any given time. The only problem I am having is that the enemies have a "Collide" script which lowers the players health if they touch this enemy.

This of course needs a reference to the player's health, but the prefab is unable to locate the player's health (you can see the bold term in the collide script) and I'm unsure as to how to automate this reference.

The health is broadly a part of a script I have where I define player health, but is there a way I can simply reference the player game object itself? In this case," Player.Canvas.Healthbar.Health " or something similar?

I know I can make the enemies find their own health bar as it is a part of the enemy's own object tree but I don't know how to refer to another object's tree.

Sorry for the noob question, I'm very much learning as I go here.

Thanksalt text

zombiecantfindhealthprefab.png (123.2 kB)
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 akaBase · May 05, 2019 at 10:15 AM 0
Share

I use a Singleton instance for Player Data and alike that can be referenced using a static call, EG: PlayerData.Instance.Health -= Damage

Tutorial with Singleton

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stewie_7999 · May 05, 2019 at 10:17 AM

Here is the code for PlayerStats in which health is defined:

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class PlayerStats : MonoBehaviour { private Image content;

 public GameObject player;

 


 [SerializeField] private Text statValue;

 [SerializeField] private float lerpSpeed;

 private float currentFill;

 private float currentValue;

 public float MyMaxValue { get; set; }

 public float MyCurrentValue
 {
     get
     {
         return currentValue;
     }
     set
     {
         if (value > MyMaxValue)
         {
             currentValue = MyMaxValue;
         }
         else if (value < 0)
         {
             currentValue = 0;
         }
         else
         {
             currentValue = value;
         }


         currentFill = currentValue / MyMaxValue;

         statValue.text = currentValue + " / " + MyMaxValue;
     }

 }

 // Start is called before the first frame update
 void Start()
 {
     MyMaxValue = 100;
     content = GetComponent<Image>();

     player = GameObject.FindGameObjectWithTag("PlayerHealth");
 }

 // Update is called once per frame
 void Update()
 {
     if (currentFill != content.fillAmount)
         content.fillAmount = Mathf.Lerp(content.fillAmount, currentFill, Time.deltaTime * lerpSpeed);
     //Debug.Log(MyCurrentValue);
 }

 public void Initialize(float currentValue, float maxValue)
 {
     MyMaxValue = maxValue;
     MyCurrentValue = currentValue;
 }

   

   

}

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 akaBase · May 05, 2019 at 10:41 AM

An extension on my comment

 public class PlayerStats : MonoBehaviour { 
 
     //Very basic Singleton - Read about thread safe ways
     public static PlayerStats Instance{get; private set;}
     
     public HealthBar healthBar; //Set this how ever you want
     
     private int _maxHealth;
     private int _health;
     public int Health {
         get{
             return _health;
         }
         set{
             _health = value;
             if(_health <= 0)
             {
                 //Out of health do something;
             }
             else
             {
                 healthBar.UpdateBar(_health / _maxHealth); // To send the ratio of health remaining to then set the fillamount on the health bar
             }
         }
     }
     
     void Awake()
     {
         //Set the instance to this instance of PlayerStats
         Instance = this;
     }
 } 

And then you update it with

 PlayerStats.Instance.Health -= Damage


I suggest that route.

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

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

Related Questions

GameObject is missing a prefab, assign prefab to gameobject 1 Answer

Canvas prefab references keep getting lost after Editor restart 1 Answer

Access object script from prefab script 1 Answer

How to obtain this prefab name in this circunstances 1 Answer

How can I apply a text variable to a prefab 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