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

find value from one class to an other in c

Discussion in 'Editor & General Support' started by ksnkfs, Dec 8, 2014.

  1. ksnkfs

    ksnkfs

    Joined:
    Nov 30, 2014
    Posts:
    35
    Hi, I know there is a lot of post asking this question but I just can't seem to find the rigt answer. I want to create a health bar by putting a lot of square one next to the other, when my life is less than a certain number, one cube is destroy and after the other and so on.

    I try two thing, first a condition in my player class that if the life valu is less than a certai nnumber, I destroy a specific actor in the scene but I did not find the way to do that.

    The second option was to create a life cube class and put a public class who dict the life condition and If the life condition is inferior to the player life, it is destroy.
    int Vie1= 0;
    public int VieRest;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    Vie1 = GetComponent<Controller_Shiplock> ().Vie;

    if (Vie1 < VieRest)
    Destroy (gameObject);
    }

    And I get an error for that., A gain I know there is a aslot of thread about it but I am very newb in the programation department so any help is appreciat.

    thank
     
  2. Select_Casey

    Select_Casey

    Joined:
    Nov 12, 2014
    Posts:
    26
    i think i see what youre trying to do. i did something similar with graphics. its for the GUI yes?

    if im right, all you really need to do is put all the health cubes in the GUI layer and rather than destroy them just set their enabled state to false when one is lost.
     
  3. ksnkfs

    ksnkfs

    Joined:
    Nov 30, 2014
    Posts:
    35
    Its to emulate a GUi but its not a GUI, I dont have any GUI class, all I do is attache cube to my player and I want them to be destroy gwhen my player lose life
     
  4. Select_Casey

    Select_Casey

    Joined:
    Nov 12, 2014
    Posts:
    26
    oh, so they follow the player around like a tail?
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  6. ksnkfs

    ksnkfs

    Joined:
    Nov 30, 2014
    Posts:
    35
    error
    line problem
    Code (CSharp):
    1.         Vie1 = GetComponent<Controller_Shiplock> ().Vie;
    2.  
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    So you have added the script from which you posted a small portion to a game object. At this very line you are asking to get the Controller_Shiplock from that game object. But the issue is that the game object doesn't have such a component (Controller_Shiplock).
    Maybe you have accidentally added the script to a wrong game object as well. If you want to find out which of the scripts throws this error, you simply need to select the error message and this will highlight the game object in the hierarchy.
     
  8. ksnkfs

    ksnkfs

    Joined:
    Nov 30, 2014
    Posts:
    35
    Controller_Shiplock is an external class and the error really come from the class I copy and past and point on the line I put there
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Where did I write there is an issue in Controller_Shiplock? What in my explanation needs to be clarified?
     
  10. ksnkfs

    ksnkfs

    Joined:
    Nov 30, 2014
    Posts:
    35
    When you write
    Anyway I gues I understand, indeed the class is not from this game object but from an other one.