[SOLVED]Using a script on multiple game objects without if affecting all of them at the same time.

So I have 5 cubes with a health script attached, On start the health value is randomized. When i use another script to start increasing the life it affects all of the gameobjects but i want the current object that im aiming towards to be affected not all of em.

i.e: i start to increase the health on cube 1, while increasing the health to the max and going on to the next cube it keeps the same value as cube1 even though its health is lets say 22 at the moment but its not working on cube2

heres the code:

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

public class BuildingHealth : MonoBehaviour {

    [SerializeField]private float Health;


    public void Start()
    {
         Health = UnityEngine.Random.Range(0, 101);      
         checkvalue(Health);        
         gameObject.GetComponent<Repairing>();
    }


    void checkvalue(float Health)
    {
        if (Health >= 85)
        {
            GetComponent<Renderer>().material.color = Color.white;
        }
        if (Health < 85 && Health > 50)
        {
            GetComponent<Renderer>().material.color = Color.yellow;
        }
        if (Health < 50 && Health > 25)
        {
            GetComponent<Renderer>().material.color = new Color(1, 0.647059f, 0);
        }
        if (Health < 25 && Health > 10)
        {
            GetComponent<Renderer>().material.color = Color.red;
        }
    }


    public void AddHealth(float RepairSpeed)
    {        
            Health += RepairSpeed;
            Debug.Log("Building Repair in progress!");
       
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Repairing : MonoBehaviour
{
    public Transform Shootpoint;
    public float RepairSpeed = 0.25f;
    public BuildingHealth health;
    RaycastHit hit;
    public float weaponRange = 15;
   

    public void Start()
    {
        gameObject.GetComponent<BuildingHealth>();

    }

    public void Update()
    {
        Debug.DrawRay(Shootpoint.position, Shootpoint.transform.forward * weaponRange, Color.green);
        if (Input.GetMouseButton(0))
        {
            if (Physics.Raycast(Shootpoint.position, Shootpoint.transform.forward, out hit, weaponRange))
            {
                if(hit.collider.gameObject.tag == "Repairable")
                {
                    if(health == null)
                    {
                        Debug.Log("health is null");
                    }
                    health.AddHealth(RepairSpeed);
                }          
            }

        }
    }

}

Here you can see that it will log the health value and it will change it only on the referenced object which i dragged from the hierarchy and that was of course cube 1. The problem is i dont know how to make the script work without affecting all the cubes the same.
There is probably an error made in the AddHealth function or i have written my script wrong.

I fixed the problem already, It turns out i had to do this in the repairing script: hit.collider.gameObject.GetComponent().AddHealth(RepairSpeed); instead of health.AddHealth(RepairSpeed);.

1 Like