Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by The Fake Ethan · Aug 30, 2015 at 03:37 PM · c#variables

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges