Search Unity

GetComponent Help.

Discussion in 'Scripting' started by Heu, Mar 18, 2012.

  1. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Hello, my name is David and i'm a bit of problem using GetComponent for C#
    I'm converting to C# from UnityScript and it's a bit confusing when i get to using GetComponent in C#.

    Can someone fix this script. Im kinda sure it would work but it keeps getting an error

    Code (csharp):
    1.                 if(other.gameObject.tag=="Player"){
    2.             scriptHealth health;
    3.             health = healthObject.GetComponent<scriptHealth>();
    4.             health.pHealth-=1;
    5.             Instantiate(bloodObject,transform.position,transform.rotation);
    6.             print ("touchedplayer");
    7.             Destroy(gameObject);        }
    what i want is, when the gameObject touchs the player, it will instantiate a prefab, and then substract health from the playerscript

    But it wont subtract the health.

    healthObject is a transform in which the script is located in
    pHealth is the int inside the script that i want to subtract from.

    EDIT; Edited.
     
    Last edited: Mar 18, 2012
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The 'player' doesn't have a 'scriptHealth' component. So your health reference is null.
     
  3. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Well do you have a reference to the healthObject inside the code.
    Or is it just a object child of the player?

    because i think you need to use
    Code (csharp):
    1.  
    2. health = other.GetComponent<scriptHealth>();
    3.  
     
  4. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Well my health script and my player script are seperate so i dont think using other would work.
     
  5. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Well are the not attach to the player??

    if so it will or should
     
  6. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    Shouldn't it be:

    Code (csharp):
    1. health = other.GetComponent<scriptHealth>()
     
  7. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    My health script is located in a health prefab because it has it's own texture and looks.

    I want if the enemy to touches the player it will subtract health from the health script In The health prefab

    If I use Other wouldnt it subtract from the player script? My player script doesnt have a health variable
     
  8. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Well first off you mention this
    And that more sounded from your players health.

    But you cannot subtract health from a prefab in script.
    So you must be explaining it wrong.
    First when you have a object of the prefab in the game you can change it.

    So maybe it the instantiate object you are talking about that you create on impact.

    And if the "prefab" you mention is in the Instantiate in you do.
    Then you have to get the object of the newly created object.

    Code (csharp):
    1.  
    2. GameObject NewObject;
    3. NewObject = Instantiate(bloodObject,transform.position,transform.rotation) as GameObject;
    4. scriptHealth health;
    5. health = NewObject.GetComponent<scriptHealth>();
    6. health.pHealth-=1;
    7.  
    I think you can even save some code by using

    Code (csharp):
    1.  
    2. scriptHealth health
    3. health = Instantiate(bloodObject,transform.position,transform.rotation) as scriptHealth;
    4. health.pHealth-=1;
    5.