- Home /
how can I reference multiple game objects at the same time in my code
alright so im making a damage system for my game that references floats to deal different amounts of damage but I've run into a problem you see I also have the player referencing the damage scripts so that the players health isn't different for all the floats but since im using this code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerHealth : MonoBehaviour
{
public float hp = 10000f;
public damage attacheddamage;
public float damagecopy;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
hp = attacheddamage.healthcopy;
}
}
I've referenced the damage as a public body I want to be able to reference all the game objects with this every single frame (because this is for enemy I'll have bullet prefabs and stuff) but im not sure how to reference all of them. I would apprietiat any help as I can't find any other way of making this type of system
Why would your damage script store any kind of health point??
Your damage script should store damage to inflict to the health script, that it. Then, the object supposed to deal damage to the health would ask the player script to decrease the HP when a certain condition occurs (collision / ...)
sounds like you are trying to make spaghetti.. Use scriptableObject architecture or Observer Pattern. Alternatively if you have only one player you could make the health static and access it anywhere.. I think you should really describe what you want to achieve and then someone can tell you how to do that rather than try to get the way you are trying to do it to work. People do that all the time they try a convoluted way of doing something and when it doesn't work they try to get someone to help them fix it when there is a better straight forward way to do the whole thing if they just described what they wanted to achieve rather than explain the method of how they are trying to do it.
In short you are going about it the wrong way and need to redesign the system.
also if you would be kind enough to maybe give me a link to a tutorial on how to ask the hp system to remove a certain amount or maybe even give me the code to go about doing all this I would be very grateful
thanks @logicandchaos you are right I know what you mean I just couldn't find anything on this because im making an fps (first person smacker cause there are no damage dealing player guns in my game) where I want the weapon to be able to deal different amounts of damage based on what bat you have and to @Hellium HUSHER im using floats in my system so the damage system has a reference to the hp and then the hp system would reference the reference making an endless cycle where both of the scripts had the same hp value also this is my first game that's got past the basics
I'll ask your to show me and everyone else some respect @EvilBob99 . Nothing in my comment justifies such disproportionate and unrespectful response.
@Hellium yes very much yes but to your suggesting that im stupid for not having known this look at your first comment and think about how it would sound for me a beginner
I never implied you were stupid. I just tried to understand the system you're implementing because I didn't make sense for me (simply because I didn't have the whole context). But even with your explanation, I also share logicandchaos' point of view. I believe you're taking the wrong path.
Answer by kuba86699 · Dec 11, 2021 at 10:56 PM
Good day. I don't fully understand your question, so I don't know if my answer will be helpful, but I will try to solve your problem. Sorry for my language right away, English is not my mother tongue so sorry if you don't understand something. In the health script, leave only public float hp = 10000f; . For bullets, create a damage script and write in it that if the bullet detects a collision with a player's object, it will be awarded health script and subtracts a certain amount of damage. I would write it something like this:
void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Player")
{
playerHealth pH = collision.collider.GetComponent<playerHealth>();
pH.hp -= 5.5f; //Set a specific value
Destroy(this.gameObject); //Destroy the bullet
}
}
Add this script to the bullet prefab. I learn programming myself, but I think that such a script should work.
it works perfectly @kuba86699 thank you very much this has made my day this has been such a frustrating problem thank you for answering
Note that this is exactly what I suggested in my first comment and is the most common way to handle damages. This has been covered in many other questions asked for years.