getting variables from other scripts C#
Hey i have been working on this script for a while and i have encountered a problem, and for the life of me i cant solve it. (using unity 5) when i run this script the TowerCanvas is always running instead of the MainCanvas and i have no idea why. i am using a raycaster to detect if the FoundHit variable is true or false. and it still works when the TowerCanvas is forcing itself on the screen. i have also made it so that when a press "Tab" MainCanvas pops up and TowerCanvas disappears, however when i click tab the Canvases switch for only one frame. i think there is something in the script forcing FoundHit to = true but at the same time i dont think so. there are only two scripts involved in the FoundHit var so there is no other script conflicting with it. if anyone knows how to help i will be so great full. here are the scripts:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class HUD : MonoBehaviour {
bool FoundHit = false;
public Slider TowerHealthSlider;
public Slider BaseHealthSlider;
int CurrantTowerHealth;
int CurrantBaseHealth;
public Canvas MainCanvas;
public Canvas TowerCanvas;
public int StartTowerHealth = 100;
public int StartBaseHealth = 25;
bool TowerDead;
bool Basedead;
public Text Ammo;
public Text EnemiesLeft;
public Text GunName;
//may have to edit these later
string CurrantAmmo = "12 / 24";
string CurrantGunName = "Colt M1911";
string CurrantEnemiesLeft = "Enemies Remaining = 10";
public void Start () {
CurrantBaseHealth = StartBaseHealth;
CurrantTowerHealth = StartTowerHealth;
Basedead = false;
TowerDead = false;
Ammo.text = CurrantAmmo;
GunName.text = CurrantGunName;
EnemiesLeft.text = CurrantEnemiesLeft;
MainCanvas.enabled = true;
TowerCanvas.enabled = false;
}
public void Update () {
GameObject Camera = GameObject.FindGameObjectWithTag ("Camera");
Clicking ClickingScript = Camera.GetComponent<Clicking>();
bool FoundHit = ClickingScript.FoundHit;
Ammo.text = CurrantAmmo;
GunName.text = CurrantGunName;
EnemiesLeft.text = CurrantEnemiesLeft;
if (FoundHit = true){
MainCanvas.enabled = false;
TowerCanvas.enabled = true;
}
if (Input.GetKeyDown (KeyCode.Tab)) {
MainCanvas.enabled = true;
TowerCanvas.enabled = false;
FoundHit = false;
}
}
public void TowerTakeDamage (int TowerAmount) {
CurrantTowerHealth -= TowerAmount;
TowerHealthSlider.value = CurrantTowerHealth;
}
public void BaseTakeDamage (int BaseAmount) {
CurrantBaseHealth -= BaseAmount;
BaseHealthSlider.value = CurrantBaseHealth;
}
}
and the other:
using UnityEngine; using System.Collections;
public class Clicking : MonoBehaviour {
public float RaycastDistance = 50;
public bool FoundHit = false;
RaycastHit Hit;
public GameObject Raycaster;
string TagCheck = "TowerBase";
void Update () {
if (Input.GetKeyDown (KeyCode.E)) {
FoundHit = Physics.Raycast (Raycaster.transform.position, transform.forward, out Hit, RaycastDistance);
if (FoundHit && Hit.transform.tag != TagCheck){
FoundHit = false;
Debug.Log ("Not the TowerBase");
}
if (FoundHit && Hit.transform.tag == TagCheck) {
Debug.Log ("You hit the TowerBase");
//TowerCanvas.enabled = true;
}
}
}
}
Thank you!
Answer by The Fake Ethan · Aug 31, 2015 at 05:47 PM
Ok i fixed this by making the Clicking script have a new variable which was used across scripts
Your answer
Follow this Question
Related Questions
Help with converting small pseudo script to C# please? 0 Answers
How to change text based on the value of a scrollbar? 1 Answer
Declared variable being returned as null in a method? 0 Answers
How can I solve this error? 1 Answer
Problem while using several tags in a OnTriggerEnter (Collider other) 0 Answers