So I have found a ton of these scripts out there but can not seem to find one for my use.
Let me explain it and show the code, maybe someone can point me in the right direction please.
I have a gameobject prefab=mineral. I have a script EmeraldTest.cs attached to it. I have duplicated this prefab and placed it into my scene at different locations.
When the gameobject prefab=player runs up to one of the mineral prefabs and starts to extract the resources from this mineral prefab, I want ONLY this prefabs resources to deduct. However,.
When the player moves to the next mineral prefab it shows this mineral prefabs correct resource amount but when I begin to extract this minerals resources this minerals original resources @ 10000 jumps down to whereever I left off from the last one say @ 5000. So this second mineral prefabs will show the 10k then instantly jump to 5k and continue drawing out the resources from 5k and down.
Ok hope this explains the problem, thx all.
PREFAB EMERALD CODE
EmeraldTest.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EmeraldTest : MonoBehaviour
{
public Rigidbody rb;
public GameObject menu; // Assign in inspector
private bool isShowing = false;
public float totalResources; // total starting resources
public float totalRsLeft; // total resources left
// public UIButton exitButton; // the exit button on the mining computer interface.
public Text RsText; // Resources Left value on UI
public ParticleSystem miningLaser; // Ref. to mining laser particle system
//bool isTriggered = false;
void Start()
{
miningLaser.Stop (); // Particles is looping so turn off on start
totalResources = Random.Range(8000,20000); // Randomize a value between (x,x)
totalRsLeft = totalResources;
menu.SetActive(isShowing); // Set the menu trigger to hide. isShowing = false;
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player") // *** SHOW MENU ***
{
//isTriggered = true;
isShowing = true; // Set the menu trigger to show. isShowing = true;
MiningComputerInterface(); // call the show menu function.
// Freeze players position in mid air.
rb.constraints = RigidbodyConstraints.FreezeAll;
RsText.text = totalResources.ToString();
}
}
void OnTriggerExit() // *** HIDE MENU ***
{
//isTriggered = false;
isShowing = false;
MiningComputerInterface(); // Hide the menu from view.
}
void MiningComputerInterface()
{
menu.SetActive(isShowing);
}
public void miningButton() // THIS IS THE CODE FOR EXTRACTING THE RESOURCES
{
miningLaser.Play();
totalRsLeft = Random.Range(20,180);
totalResources = totalResources - totalRsLeft;
RsText.text = totalResources.ToString();
miningLaser.Stop();
}
public void exitButtonPressed()
{
rb.constraints = RigidbodyConstraints.None;
rb.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
}
}