Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change a Variable of a script attached to a Instantiated Object

Discussion in 'Scripting' started by Ruuds, Dec 12, 2018.

  1. Ruuds

    Ruuds

    Joined:
    Dec 3, 2017
    Posts:
    17
    Hi Guys,

    I am Instantiating a object in front of my Character to attack, I would like to change the Damage Value based of my Characters stats this is what my Code Looks like:

    Code (csharp):
    1.      
    2. private void Combat()
    3.     {
    4.         GameObject clone;
    5.         if(attackCooldown == false)
    6.         {
    7.             if (Input.GetKey(KeyCode.Space))
    8.             {
    9.                
    10.                 print("Player Starts Attack");
    11.                 playerState = State.Attack;
    12.                 StartCoroutine(PlayerAttack());
    13.                 GetComponent<Scr_Character_Status>().xVelocity = 0;
    14.                 GetComponent<Scr_Character_Status>().yVelocity = 0;
    15.                 rb2d.velocity = new Vector2(0, 0);
    16.                 if (playerDirection == 1)
    17.                 {
    18.                     if (WeaponMelee)
    19.                     {
    20.                                   clone = (GameObject)Instantiate(ObjAttack, transform.position + (transform.up * 1), transform.rotation);
    21.                         Scr_AttackMelee AttackScript = clone.GetComponent<Scr_AttackMelee>();
    22.                         AttackScript.attackDamageMelee = 88;
    23.                     }
    24.  
    25.                 }
    26.             }
    27.         }  
    28.     }
    29.  

    This is the Code on the Object Im instantiating


    Code (csharp):
    1.  
    2. public class Scr_AttackMelee : MonoBehaviour {
    3.  
    4.     public float attackDamageMelee;
    5.  
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         StartCoroutine(Delete());
    10.     }
    11.    
    12.     IEnumerator Delete (){
    13.         yield return new WaitForSeconds(5);
    14.         Destroy(this.gameObject);
    15.     }
    16. }
    17.  
    I get no error the Variable on the Instantiate object just wont change to 88. Im a beginner in programming so Im guessing I am just missing something obvious.

    Thank you
     
  2. flogma25

    flogma25

    Joined:
    Oct 10, 2018
    Posts:
    11
    Hello,
    Maybe you can add this :
    Debug.Log("attackDamageMelee : " + AttackScript.attackDamageMelee);




    before and after the work ?

    Like this :

    Code (CSharp):
    1. private void Combat()
    2.     {
    3.         GameObject clone;
    4.         if(attackCooldown == false)
    5.         {
    6.             if (Input.GetKey(KeyCode.Space))
    7.             {
    8.              
    9.                 print("Player Starts Attack");
    10.                 playerState = State.Attack;
    11.                 StartCoroutine(PlayerAttack());
    12.                 GetComponent<Scr_Character_Status>().xVelocity = 0;
    13.                 GetComponent<Scr_Character_Status>().yVelocity = 0;
    14.                 rb2d.velocity = new Vector2(0, 0);
    15.                 if (playerDirection == 1)
    16.                 {
    17.                     if (WeaponMelee)
    18.                     {
    19.                                   clone = (GameObject)Instantiate(ObjAttack, transform.position + (transform.up * 1), transform.rotation);
    20.                         Scr_AttackMelee AttackScript = clone.GetComponent<Scr_AttackMelee>();
    21. Debug.Log("attackDamageMelee : " + AttackScript.attackDamageMelee);
    22.                         AttackScript.attackDamageMelee = 88;
    23. Debug.Log("attackDamageMelee : " + AttackScript.attackDamageMelee);
    24.                     }
    25.                 }
    26.             }
    27.         }
    28.     }
    and watch what append ?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    What type of gameobject is the clone? How are you verifying the value isn't changing?

    If it's some sort of text object, changing the variable alone doesn't change the visual text. You'll need to update that in code. I would suggest instead of changing the variable outright, you pass a value to a method that changes the value and then updates some visual element (changing text).

    Now, this is assuming what you have is some popup text to display damage.