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

Help with UI Thing

Discussion in 'Scripting' started by Piracase, Apr 6, 2015.

  1. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Hello guyz,
    I was learning making games as Adam told me to go to www.unity3d.com/learn.
    The problem is that in the survival shooter tutorial ,there is a script named "PlayerHealth" i thought to check it
    but i get this error,
    Assets/Scripts/PlayerHealth.cs(2,19): error CS0138: `UnityEngine.UI' is a type not a namespace. A using namespace directive can only be applied to namespaces



    Help,Code is
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Collections;
    6.  
    7. public class PlayerHealth : MonoBehaviour
    8. {
    9.     public int startingHealth = 100;                            // The amount of health the player starts the game with.
    10.     public int currentHealth;                                   // The current health the player has.
    11.     public Slider healthSlider;                                 // Reference to the UI's health bar.
    12.     public Image damageImage;                                   // Reference to an image to flash on the screen on being hurt.
    13.     public AudioClip deathClip;                                 // The audio clip to play when the player dies.
    14.     public float flashSpeed = 5f;                               // The speed the damageImage will fade at.
    15.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);     // The colour the damageImage is set to, to flash.
    16.  
    17.  
    18.     Animator anim;                                              // Reference to the Animator component.
    19.     AudioSource playerAudio;                                    // Reference to the AudioSource component.
    20.     PlayerMovement playerMovement;                              // Reference to the player's movement.
    21.     PlayerShooting playerShooting;                              // Reference to the PlayerShooting script.
    22.     bool isDead;                                                // Whether the player is dead.
    23.     bool damaged;                                               // True when the player gets damaged.
    24.  
    25.  
    26.     void Awake ()
    27.     {
    28.         // Setting up the references.
    29.         anim = GetComponent <Animator> ();
    30.         playerAudio = GetComponent <AudioSource> ();
    31.         playerMovement = GetComponent <PlayerMovement> ();
    32.         playerShooting = GetComponentInChildren <PlayerShooting> ();
    33.  
    34.         // Set the initial health of the player.
    35.         currentHealth = startingHealth;
    36.     }
    37.  
    38.  
    39.     void Update ()
    40.     {
    41.         // If the player has just been damaged...
    42.         if(damaged)
    43.         {
    44.             // ... set the colour of the damageImage to the flash colour.
    45.             damageImage.color = flashColour;
    46.         }
    47.         // Otherwise...
    48.         else
    49.         {
    50.             // ... transition the colour back to clear.
    51.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    52.         }
    53.  
    54.         // Reset the damaged flag.
    55.         damaged = false;
    56.     }
    57.  
    58.  
    59.     public void TakeDamage (int amount)
    60.     {
    61.         // Set the damaged flag so the screen will flash.
    62.         damaged = true;
    63.  
    64.         // Reduce the current health by the damage amount.
    65.         currentHealth -= amount;
    66.  
    67.         // Set the health bar's value to the current health.
    68.         healthSlider.value = currentHealth;
    69.  
    70.         // Play the hurt sound effect.
    71.         playerAudio.Play ();
    72.  
    73.         // If the player has lost all it's health and the death flag hasn't been set yet...
    74.         if(currentHealth <= 0 && !isDead)
    75.         {
    76.             // ... it should die.
    77.             Death ();
    78.         }
    79.     }
    80.  
    81.  
    82.     void Death ()
    83.     {
    84.         // Set the death flag so this function won't be called again.
    85.         isDead = true;
    86.  
    87.         // Turn off any remaining shooting effects.
    88.         playerShooting.DisableEffects ();
    89.  
    90.         // Tell the animator that the player is dead.
    91.         anim.SetTrigger ("Die");
    92.  
    93.         // Set the audiosource to play the death clip and play it (this will stop the hurt sound from playing).
    94.         playerAudio.clip = deathClip;
    95.         playerAudio.Play ();
    96.  
    97.         // Turn off the movement and shooting scripts.
    98.         playerMovement.enabled = false;
    99.         playerShooting.enabled = false;
    100.     }    
    101. }
    102.  
    Please Note That Im using Unity 4 it dosent have UI option,Help me to fix the error please.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    Remove line 4?
     
  3. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Dont you think it will ruin the work of the script?
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,683
    I have not gotten far into Unitys UI. I still use EZGUI. Tho, I think I tried it briefly once,and do not recall it requiring a using...

    That being said, erase it. See what happens. There is always an undo button.
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    What version of unity are you using? Could you be using an old or obsolete version? UnityEngine.UI has been included since 4.6, which implies you are using. 4.5 or earlier.
     
    Kiwasi likes this.
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes, you do need it.
     
  7. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    I'm using Unity 4,how can fix that error please :(
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Unity ... 4.... What? 4.1? 4.2? 4.3? 4.4? 4.5? 4.6?

    Please read my post above.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You must upgrade to Unity 4.6 or later to use the UI tools.

    Otherwise you have to use legacy GUI or a third party tool.
     
  10. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
  11. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    Can i have a download link of Unity 4?
     
  12. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Its clearly marked on the site, if you grab 4.6 you should be able to upgrade your project with out breaking anything.
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Why not just do this in 5.0?
     
    Kiwasi likes this.
  14. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    I did Adam,But when it's installing it took whole day,And i quit the installation
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    A whole day? Then there's something wrong somewhere... Unity5 is under 5gb all in. Unity 4 is only a few mb smaller and is more like 4.5gb. I can install on a machine from 2008 in minutes. Can you tell me more about your setup? You will need either the latest 4.6 or 5.0. Both are about the same size... so you'll have to install one of them to get the new UI. I'd suggest Unity 5.
     
  16. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    I downloaded Unity5 installer,When it downloads it's file it too kwhole day
     
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Very slow connection? Well, once you have the data, the install should he fast. Either way, you'll need an up to date copy of unity, either 4 or 5.
     
  18. Piracase

    Piracase

    Joined:
    Mar 29, 2015
    Posts:
    31
    How can i get the Copy?
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664