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 /
This question was closed Feb 11, 2016 at 01:12 PM by seevenup for the following reason:

i place a new question which should be more clear

avatar image
0
Question by seevenup · Feb 08, 2016 at 11:31 PM · variablefunction

How to pass a variabe to OnGUI() (could need Help)

Hi there, Im very new to Unity and C+ (2 week experion) but i dont get why my OnGUI gets a wrong value back

I have the public function, that i call for changing the "score" value when a GameObject is deatroyed.

 ShowScore.Scoring ();

and the function

 private int Score = 1;
 public void Scoring(){
         Score = 3;
         Debug.Log (Score);
 }

now when the function is called it shows in DebugLog 3. But my OnGUI which is in the same script gives me in the DebugLog 1 back.

     void OnGUI()
     {
         GUI.Label(rect, "Score: "+Score, style);
         Debug.Log (Score);
     }

I changed the integer from private to public, but still the same Problem. What i am doing wrong?

I tried now for 3h i dont get it. So any help would be awesome

Why im doing this: I have a GameObject which reports to the ShowScore Script when it is destroyed and it should add 10 points to the Score variable which are shown in the OnGUI

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

2 Replies

  • Sort: 
avatar image
0

Answer by karl_jones · Feb 09, 2016 at 12:05 AM

Do you have 2 scripts in the world? Each one is an instance, so if you have 2 scripts you have 2 different values for Score.

Post your full script.

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
avatar image
0

Answer by seevenup · Feb 09, 2016 at 05:27 PM

so this is my code (i know its not pretty)

This is my HealhBarMonster script which shows the healthbar over the enemy. At the end i call the function to try add 10 points to the Score on the GUI using UnityEngine; using System.Collections;

 public class HealthBarMonster : MonoBehaviour {
 
     public float MaxHhealth = 100f;
     public float CurrentHealth = 0f;
     public float Score = 10;
     public GameObject healthBar;
     public Tower Tower;
     public ShowScore ShowScore;
 
     // Use this for initialization
     void Start () {
         CurrentHealth = MaxHhealth;
 
     }
     
     public void decreasehealth(float dmg){
         CurrentHealth -= dmg;
         float calc_health = CurrentHealth / MaxHhealth;
         SetHealthBar (calc_health);
     }
 
     public void SetHealthBar(float health){
         healthBar.transform.localScale = new Vector3 (Mathf.Clamp (health, 0f, 1f), healthBar.transform.localScale.y, healthBar.transform.localScale.z);
         if (CurrentHealth <= 0) {
             Tower.DeathReport ();
             ShowScore.Scoring ();
             Destroy (gameObject);
         }
     }
 }

and here i try to set the Score to 3 (proof of concept)

 public class ShowScore : MonoBehaviour {
 
     //float ScoreCount;
     private int Score = 1;
        
     public void Scoring(){
         Score = 3;
         Debug.Log (Score);
     }
  
     void OnGUI()
     {
         int w = Screen.width, h = Screen.height;
         GUIStyle style = new GUIStyle();
         Rect rect = new Rect(0, 10, w, h * 2 / 100);
         style.alignment = TextAnchor.UpperLeft;
         style.fontSize = h * 2 / 100;
         style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
         GUI.Label(rect, "Score: "+Score, style);
         Debug.Log (Score);
     }
 }

now i see in the DebugLog that when the function is called " ShowScore.Scoring ();" i get in the DebugLog 3 back. But on the GUI it shows me 1. I tried to change from private int to pulic int, but did not help. i think im missing something i dont see

EDIT: It looks that i really have 2 diffrent variable. When i call a function located in a other script, it does not have the same value, as when i call it in the same script.

how can i change this @karl.jones

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

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need to activate a function in another script from a script on a different game object 0 Answers

How to give Vector3 position to object being instantiated 1 Answer

Changing a variable in another script with raycast 0 Answers

Only plus one to variable in other script 1 Answer

Problem with decrement 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