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. Dismiss Notice

Ammo Script Problems. :)

Discussion in 'Scripting' started by Destrark, Jul 12, 2016.

  1. Destrark

    Destrark

    Joined:
    Jul 12, 2016
    Posts:
    15
    Hiya. Having problems setting up a ammo script for the GUI text I've attached to the camera to track ammunition (tennis balls).
    This is the shooter script:

    Code (csharp):
    1. public class Shooter : MonoBehaviour {
    2.  
    3. public Rigidbody bullet;
    4. public float power = 1500f;
    5. public float moveSpeed = 2f;
    6.  
    7. void Update (){
    8. if(Input.GetButtonUp("Fire1")){
    9.  
    10. Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
    11.  
    12. Vector3 fwd = transform.TransformDirection(Vector3.forward);
    13. instance.AddForce(fwd * power);  }
    14. }
    15. }
    And this is the Ball Counter script:

    Code (csharp):
    1. public class BallCounter : MonoBehaviour {
    2.  
    3. public int ammo = 10;
    4. public uitext;
    5. void Update() {
    6.   if(uitext != null) {
    7. uitext.text = ammo.ToString();
    8. GameObject.Find("Ammo_Counter").GetComponent("Ball_Counter").text = myCounter.ToString() + " / 10";
    9. }
    10. }
    11. }
    I'm not familiar enough with coding to be able to figure out what I'm doing wrong here unfortunately. Trying to build the script so I have a maximum with ten shots firing the Tennis Ball prefab I've attached to the shooter script.
    If someone can help me then it would be gratefully appreciated.
     
    Last edited: Jul 12, 2016
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1. public uitext;
    no type for this declaration... and if you want to use canvas classes you need
    Code (csharp):
    1.  
    2. using UnityEngine.UI;
    3.  
    at the top of the file, then the type is just "Text"
     
  4. Destrark

    Destrark

    Joined:
    Jul 12, 2016
    Posts:
    15
    changed
    Code (csharp):
    1. public uitext
    into
    Code (csharp):
    1. using UnityEngine.UI;
    But don't understand what you mean with
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    er, no...
    by "at the top of the file" I mean, at the top of the script you are writing with all the other "using blahblahblah"


    basics of declaring a variable
    [accessor][type][variablename];
    so
    Code (csharp):
    1.  
    2. public Text uitext;
    3.  
     
  6. Destrark

    Destrark

    Joined:
    Jul 12, 2016
    Posts:
    15
    Okay, heres current version:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. using System.Collections;
    5.  
    6. using UnityEngine.UI;
    7.  
    8. public class BallCounter : MonoBehaviour {
    9.  
    10. public int ammo = 10;
    11.  
    12. public Text uitext;
    13.  
    14. void Update() {
    15.  
    16.   if(uitext != null) {
    17.  
    18. uitext.text = ammo.ToString();
    19.  
    20. GameObject.Find("Ammo_Counter").GetComponent("Ball_Counter").text =
    21.  
    22. myCounter.ToString() + " / 10";
    23.  
    24. }
    25.  
    26. }
    27.  
    28. }
    problem with Text from 'public Text uitext' the type couldn't be found. And for 'UnityEngine.UI', UI doesn't exist in the namespace UnityEngine.
     
    Last edited: Jul 12, 2016
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    Look at your own post. Does that look legible to you? It's really hard to help you if we can't read your code!

    Anyways, what version of Unity are you on? UnityEngine.UI has been around for well over a year now. If you get an "UI doesn't exist in UnityEngine" error, it sounds like you're on a Unity version that's older than 4.6.
     
  8. Destrark

    Destrark

    Joined:
    Jul 12, 2016
    Posts:
    15
    I think mine's U 4.5? was planning to get it updated once I didn't need it for latest project (S*** download rate at home).
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    The GUI text system doesn't exist in 4.5, so it'll be pretty hard for you to use it!

    You can either use the old OnGUI system, or upgrade to a newer Unity version (5.4 is the latest). Note that there were a bunch of breaking changes from Unity 4 to 5.

    You can also get the latest version of 4 if you dig around on the download sections.