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

Change variable of component added on runtime.

Discussion in 'Scripting' started by raycosantana, Apr 12, 2014.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    is it possible to do so? because so far it seems it doesn't or at least I cant figure out to do so. :rolleyes:

    Basically the component Im adding is a Damage Pop up script that destroys itself after a second but problem is I cant use getcomponent to change the variable because there are multiple copies of this component per object and each one needs to have a different value on this variable (received damage).

    Kind regards.
     
    Last edited: Apr 12, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Sure it is. Use the return value of AddComponent, such as

    Code (csharp):
    1. var script = AddComponent(SomeScript);
    2. script.someVar = 99;
    --Eric
     
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Oh lol! I didnt had a variable to store the return value to it just gameObject.AddComponent("DamagePop"); :D


    Thanks I will try this ASAP!