Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Displaying a variable on the screen (C#?)

Discussion in 'Scripting' started by nicole.dewinter, Nov 30, 2013.

  1. nicole.dewinter

    nicole.dewinter

    Joined:
    Nov 6, 2013
    Posts:
    10
    I've been searching this for hours and I'm not making any progress. I have a default script that spawns an object when the first mouse button is clicked. You can set a 'clip size' that limits the amount of objects that can be spawned at once. I want to display this changing number on the screen, like ammo.

    The script I have was given to me. I don't know much c# but I understand it pretty well.

    This is the script that spawns the object:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObjectSpawner : MonoBehaviour
    5. {
    6.     //This is the link to the object that we will spawn
    7.     public GameObject objectToSpawn;
    8.     public float force;
    9.    
    10.     public int clipSize;
    11.     public float reloadTime;
    12.    
    13.     public float currentReloadTime;
    14.     public int currentClipSize;
    15.  
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.         currentClipSize = clipSize;
    20.         currentReloadTime = 0f;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         //If our left mouse button is clicked and we have at least one ammo in the clip
    27.         if (Input.GetMouseButtonDown(0)  currentClipSize > 0)
    28.         {
    29.             //Creates a new object at our current position and rotation
    30.             GameObject myObject = Instantiate(objectToSpawn, transform.position + Camera.main.transform.forward, transform.rotation) as GameObject;
    31.             myObject.rigidbody.AddForce(Camera.main.transform.forward * force, ForceMode.Impulse);
    32.            
    33.             //Take away one from the ammo
    34.             currentClipSize = currentClipSize - 1;
    35.         }
    36.        
    37.         //If we're out of ammo, start reloading
    38.         if(currentClipSize == 0)
    39.         {
    40.             currentReloadTime += Time.deltaTime;
    41.            
    42.             //Wait until we complete the reload time before returning all the ammo
    43.             if(currentReloadTime > reloadTime)
    44.             {
    45.                 currentReloadTime = 0f;
    46.                 currentClipSize = clipSize;
    47.             }
    48.         }
    49.    
    50.     }
    51. }
    52.  
    I tried to make a separate script but it has a compiling error:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ObjectSpawner : MonoBehaviour
    6. {
    7.  
    8.     public int clipSize;
    9. //Defining the clip size
    10.    
    11.     void Update()
    12.     {
    13.     //If the first mouse button is pressed and the variable clipSize is greater than one, then..
    14.         if(Input.GetMouseButtonDown(0)  clipSize > 0)
    15.         {
    16.             clipSize = clipSize - 1;
    17.             //take one away from variable clipSize
    18.             }
    19.         GUI.Label(new Rect(10,10,100,20), clipSize.ToString());
    20.         }
    21.     }
    22.  
    I thought it might be as simple as adding a line of text that displays it but no matter what I do I get multiple errors.
    Can anyone point me in the right direction?

    Sorry about the formatting. New to all this.
     
  2. THoeppner

    THoeppner

    Joined:
    Oct 10, 2012
    Posts:
    205
    If you want to draw something on the GUI you have to do this in the OnGUI method every MonoBehaviour inherits like in the following code:

    Code (csharp):
    1.  
    2.  
    3. void OnGUI()
    4. {
    5.     GUI.Label(new Rect(10,10,100,20), clipSize.ToString());
    6. }
    7.  
    8.  
    One tip: if you want that someone helps you it would be good if you give as much information as you can. E.g: You can also list the error messages you get. Otherwise its difficult to help you.
     
  3. nicole.dewinter

    nicole.dewinter

    Joined:
    Nov 6, 2013
    Posts:
    10
    Thankyou for your reply!
    I'm getting an error;
    "Keyword 'void' cannot be used in this context"
     
  4. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Could you please show your code? It sounds like you've put something in the wrong place.
     
  5. nicole.dewinter

    nicole.dewinter

    Joined:
    Nov 6, 2013
    Posts:
    10
    I added it to the first script;

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ObjectSpawner : MonoBehaviour
    6. {
    7.     //This is the link to the object that we will spawn
    8.     public GameObject objectToSpawn;
    9.     public float force;
    10.    
    11.     public int clipSize;
    12.     public float reloadTime;
    13.    
    14.     public float currentReloadTime;
    15.     public int currentClipSize;
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.         currentClipSize = clipSize;
    21.         currentReloadTime = 0f;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         //If our left mouse button is clicked and we have at least one ammo in the clip
    28.         if (Input.GetMouseButtonDown(0)  currentClipSize > 0)
    29.         {
    30.             //Creates a new object at our current position and rotation
    31.             GameObject myObject = Instantiate(objectToSpawn, transform.position + Camera.main.transform.forward, transform.rotation) as GameObject;
    32.             myObject.rigidbody.AddForce(Camera.main.transform.forward * force, ForceMode.Impulse);
    33.            
    34.             //Take away one from the ammo
    35.             currentClipSize = currentClipSize - 1;
    36.         }
    37.        
    38.         //If we're out of ammo, start reloading
    39.         if(currentClipSize == 0)
    40.         {
    41.             currentReloadTime += Time.deltaTime;
    42.            
    43.             //Wait until we complete the reload time before returning all the ammo
    44.             if(currentReloadTime > reloadTime)
    45.             {
    46.                 currentReloadTime = 0f;
    47.                 currentClipSize = clipSize;
    48.             }
    49.         }
    50.     void OnGUI()
    51.         {
    52.             GUI.Label(new Rect(10,10,100,20), currentClipSize.ToString());
    53.     }
    54. }
    55.  
     
  6. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You've put the OnGUI function inside the Update function, move it outside Update and it should work. If you take a close look to the curly braces { } you can see where a codeblock starts/ends.

    Wrong:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.   void OnGUI()
    5.   {
    6.    
    7.   }
    8. }
    9.  
    Good:

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.  
    5. }
    6.  
    7. void OnGUI()
    8. {
    9.  
    10. }
    11.  
     
    Last edited: Nov 30, 2013
  7. nicole.dewinter

    nicole.dewinter

    Joined:
    Nov 6, 2013
    Posts:
    10
    It WORKS!
    Thankyou so much!
     
  8. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You're welcome :)