I’ve got an issue that I am having a bit of a struggle with… It seems super simple as I can get it to work on the player, but when I try to implement it in a different way it isn’t working out…
I have three types of objects: The player, a health box, and many injured boxes.
I want to push a health box onto an injured box to heal it.
I use a public static variable for my player, but doing this for all the injured boxes would heal all of them regardless of the one I push the cube on to. I’m kinda not grasping what to do to get the injured boxes health variable and add the health pickup to it. Also, how do I determine which injured box’s variable I am referencing?
I have been reading about finding the nearest object with tag, get component etc… I just need a nudge in the proper direction.
So far I am using the OnTriggerEnter, and an if(collider.gameObject.name==“BuildingBox”)
I’m stuck with trying to add the health amount to the “BuildingBox” as I do the players.
Sorry if this is a bit convoluted in my explanation. I Can post the script that I use for my player, but it uses public static variables… so not much help…
public static variable, means only one exists, so that makes sense when you say it heals all the boxes.
you need to use OnTriggerEnter() or OnCollisionEnter()
You need to post your code to get a better answer.
This depends on how the other boxes are instantiated and what the script actually looks like. If each injured box contains a reference to the class, it won’t matter if the var is static or not - it will always heal them all. If each injured box is an instance of that class (or possibly a copy of a classful object), then they will take individual healing.
Thanks for the replies… Sorry I didn’t respond sooner… I had a long busy weekend.
The code that is on the “powerup” is posted below. I believe it would work if I could get the message sent to the method in the buildings script. I am not sure exactly how to do that however. I use the sendMessage to damage the box using raycast and a collider which works as expected. I am just not sure what to put at the gameObject.SendMessage line to communicate refuel to the RefuelingBuilding method.
public class RefueledTheBuilding{
public float refuelAmount;
}
public class RefuelBuilding : MonoBehaviour {
public float refuelValue = 100.0f;
void OnTriggerEnter(Collider collider){
if(collider.gameObject.tag == "Building") {
Debug.Log("Powering up a Building!!");
RefueledTheBuilding refuel = new RefueledTheBuilding();
refuel.refuelAmount = refuelValue;
gameObject.SendMessage("RefuelingBuilding", refuel,SendMessageOptions.DontRequireReceiver);
}
}
}
…Below is a snippet of code from the building/box’s script that I am trying to powerup/heal. This is the Method that I want the refuel.refuelAmount to be applied to. When I run the game the Debug from the RefueledTheBuilding script appears, but the two Debugs in snippet below are never reached.
RefuelingBuilding is not a public function. so changing that would help
this still might not work. but it might point you in the right direction. if the collider object is the gameobject that has building/box’s script then do a getCommonent<>().RefuelingBuilding(refuel.refuelAmount);
void OnTriggerEnter(Collider collider){
if(collider.gameObject.tag == "Building") {
Debug.Log("Powering up a Building!!");
RefueledTheBuilding refuel = new RefueledTheBuilding();
refuel.refuelAmount = refuelValue;
collider.getCommonent<building/box's script>().RefuelingBuilding(refuel.refuelAmount);
//or try this
building/box's script.RefuelingBuilding(refuel.refuelAmount);
//gameObject.SendMessage("RefuelingBuilding", refuel,SendMessageOptions.DontRequireReceiver);
}
}
Hey Johne… no, at least not the way I have tried based off of your reply. I’m still quite a noob so I get lost pretty easy with certain things.
I’ll be able to spend more time on it in a couple of hours. Seems to me that I should be able to use sendMessage to send the refuelAmount to the building script and subtract it in void RefuelingBuilding. I just don’t know how to send that message. It works when I raycast on my weapon script, but I am sending the message via the collider that is hit.
Below are the two scripts… The first script is called BuildingLifeAttributes. This is used on all Prefab Buildings that can be both damaged and healed. The second script is called RefuelBuilding. This script is applied to prefabs placed throughout the test level I am constructing. These objects are fuel/health packs for the buildings. The player pushes them up to a building and when the fuel/health pack collides with a building it triggers the building to gain more fuel/health. The trigger works as expected but like I stated above… The BuildingLifeAttributes is not receiving the SendMessage as the Debug.Log’s never pop up in console at runtime.
public class RefueledTheBuilding{
public float refuelAmount;
}
public class RefuelBuilding : MonoBehaviour {
public float refuelValue = 100.0f;
void OnTriggerEnter(Collider collider){
if(collider.gameObject.tag == "Building") {
Debug.Log("Powering up a Building!!");
RefueledTheBuilding refuel = new RefueledTheBuilding();
refuel.refuelAmount = refuelValue;
SendMessage("RefuelingBuilding", refuel,SendMessageOptions.DontRequireReceiver);
}
}
}
I believe that where this is not working is that the SendMessage is not looking for the GameObject that triggered the collision. I am uncertain what should go before the SendMessage. The method just above RefuelingBuilding uses identical code except that a Raycast is used and the message sent looks like the below code. hit is the the variable name that stores the Raycast in that case.
Hi Kory,
This is what I would do. It works in my test scene. When the refueler hits the trigger on the building. the building go to 200 health.
using UnityEngine;
using System.Collections;
public class RefueledTheBuilding
{
public float refuelAmount;
}
public class RefuelBuilding : MonoBehaviour
{
public float refuelValue = 100.0f;
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.tag == "Building")
{
Debug.Log("Powering up a Building!!");
RefueledTheBuilding refuel = new RefueledTheBuilding();
refuel.refuelAmount = refuelValue;
collider.GetComponent<BuildingLifeAttributes>().RefuelingBuilding(refuel);
//SendMessage("RefuelingBuilding", refuel, SendMessageOptions.DontRequireReceiver);
}
}
}
using UnityEngine;
using System.Collections;
public class BuildingLifeAttributes : MonoBehaviour
{
private float currentBuildingFuelpoints;
public float buildingFuelPoints = 100.0f;
void Awake()
{
currentBuildingFuelpoints = buildingFuelPoints;
}
void Start()
{
}
void Update()
{
if (currentBuildingFuelpoints <= 0)
{
KillBuilding();
}
}
public void BuildingTakesHit()
{
if (currentBuildingFuelpoints <= 0.0f)
{
currentBuildingFuelpoints = 0.0f;
KillBuilding();
}
}
void EnemyDamagedBuilding(EnemyShotBuilding gunHit)
{
currentBuildingFuelpoints -= gunHit.enemyDamage;
BuildingTakesHit();
}
public void RefuelingBuilding(RefueledTheBuilding refuel)
{
currentBuildingFuelpoints += refuel.refuelAmount;
Debug.Log("AmountApplied to Refuel Building" + refuel.refuelAmount);
Debug.Log("ThisBuildingsHealth" + currentBuildingFuelpoints);
}
public void KillBuilding()
{
Debug.Log("BuildingDies PlayerDoesNot");
Destroy(gameObject);
}
}