So i have made some scripts which i think should work but aren’t working. And i think i know what the problem is but i dont know why nor how to solve that problem.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuildingHealth : MonoBehaviour {
public float Health;
public float Rate = 1f;
public Image Oxygenbar;
public bool makeoxygen = false;
public void Start()
{
Health = UnityEngine.Random.Range(0, 101);
checkvalue(Health);
}
public 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)
{
GetComponent<Renderer>().material.color = Color.red;
}
}
public void AddHealth(float RepairSpeed)
{
Health += RepairSpeed;
Debug.Log(Health);
if(Health >= 100)
{
Health = 100;
makeoxygen = true;
}
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)
{
GetComponent<Renderer>().material.color = Color.red;
}
}
private void Update()
{
}
}
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 Switchweapon weapon;
public float Rate = 1f;
public OxygenCheck oxygen;
public void Start()
{
gameObject.GetComponent<BuildingHealth>();
weapon = GetComponent<Switchweapon>();
gameObject.GetComponent<OxygenCheck>();
}
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 == "Chipbuilding")
{
if (gameObject.GetComponent<Switchweapon>().Weldertrue == true)
{
hit.collider.gameObject.GetComponent<BuildingHealth>().AddHealth(RepairSpeed);
}
else if (hit.collider.gameObject.tag == "Wrenchbuilding")
{
if (gameObject.GetComponent<Switchweapon>().Wrenchtrue == true)
{
hit.collider.gameObject.GetComponent<BuildingHealth>().AddHealth(RepairSpeed);
}
}
}
}
}
oxygen.CallProduceOxygen();
// gameObject.GetComponent<OxygenCheck>().CallProduceOxygen(); gives null error
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProduceOxygen : MonoBehaviour {
public float Oxygen = 0f;
public Image oxygenbar;
public void AddOxygen(float Rate)
{
Oxygen += Rate;
oxygenbar.fillAmount = Oxygen / 100;
Debug.Log(Oxygen);
if (Oxygen >= 100)
{
Oxygen = 100;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OxygenCheck : MonoBehaviour
{
public float weaponRange = 5;
// public Transform Shootpoint;
RaycastHit hit;
float Rate = 1f;
public ProduceOxygen oxygen;
public BuildingHealth health;
// Use this for initialization
void Start()
{
gameObject.GetComponent<ProduceOxygen>();
//gameObject.GetComponent<BuildingHealth>();
}
// Update is called once per frame
public void CallProduceOxygen()
{
if (health.makeoxygen == true)
if(oxygen.Oxygen < 100)
oxygen.AddOxygen(Rate);
// gameObject.GetComponent<ProduceOxygen>().AddOxygen(Rate);
}
}
I tried working around the problem with the last 2 scripts but it wont really work.
What im trying to do is: Health on buildings get randomized, player goes to repair one till the health is 100, then (this is the part im trying to achieve) i check if health is 100 and tell the other script to start producing oxygen. Now for a single object it kinda worked. But im trying to achieve it for multiple objects. I have 5 of em.
I dont know how to work around this problem because i cant reference the BuildingHealth component anywhere it will give null.