Search Unity

combining variable values

Discussion in 'Scripting' started by BlitheD, Oct 19, 2006.

  1. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    I am trying to combine the values of two variables that I am getting from two different scripts and combining in a third.
    I am using GetComponent but am running into trouble. I thought something like this below might work but though Untiy doesn't object to the script when I save it, it tells me "Object reference not set to an instance of an object".
    Could anyone tell me where I need to alter this script to get it to work (or if I should scrap it and start over?)
    Thanks. Here's the script:

    var getA = GetComponent ("/Aobject.Ascript.A");
    getA *= 100;
    var getB = GetComponent ("/Bobject.Bscript.B");
    getB *= 100;
    getA += getB;
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Not sure exactly what you want to do, but if you do not want to modify the variables in the referenced scripts:
    Code (csharp):
    1.  
    2. var getA : float = GetComponent(ScriptA).variableA;
    3. var getB : float = GetComponent(ScriptB).variableB;
    4.  
    5. getA *= 100.0;
    6. getB *= 100.0;
    7. getA += getB;
    8.  
    You could also declare a public variable and just drag and drop you referenced scripts, that way you do not need to worry about getting them at run time everytime you need to access them.
    Code (csharp):
    1.  
    2. var ScriptA : ScriptA;
    3.  
    then your reference would be
    Code (csharp):
    1.  
    2. getA = ScriptA.variableA;
    3.