Search Unity

HELP! Adding values to variables in another script

Discussion in 'Scripting' started by Buklau123, Aug 1, 2014.

  1. Buklau123

    Buklau123

    Joined:
    Aug 1, 2014
    Posts:
    4
    Hello. I am having a lot of trouble with scripting. I want to add values to a variable in a script when a function is activated in another script.

    This is the script that has the function that is supposed to add values to a variable in another script when it's function is used:

    var papercount = 0;

    function OnTriggerEnter (other : Collider)
    {
    Destroy(other.gameObject);
    papercount = papercount + 1;
    Otherscriptname.MoveSpeed = MoveSpeed + 2
    }

    This is the script with the variable that we want to add a value to everytime the function from the other script is used:

    var Player : Transform;
    var MoveSpeed = 4;
    var MaxDist = 10;
    var MinDist = 5;



    function Start ()
    {

    }

    function Update ()
    {
    transform.LookAt(Player);

    if(Vector3.Distance(transform.position,Player.position) >= MinDist){

    transform.position += transform.forward*MoveSpeed*Time.deltaTime;



    if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
    {
    //Here Call any function U want Like Shoot at here or something
    }

    }
    }

    The second script is originally from someone else that I copy and pasted, and it works nicely without the script with the function. Please help me! I don't know much about coding!
     
  2. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
  3. Buklau123

    Buklau123

    Joined:
    Aug 1, 2014
    Posts:
    4
    THANK YOU SO MUCH! The scripts are actually on two different objects, but it works by using the GameObject.GetComponent command! Thanks again! You really helped me out! I spent LOADS of time trying to figure it out, and FINALLY I can continue to progress on my game!
     
    GarthSmith likes this.
  4. Buklau123

    Buklau123

    Joined:
    Aug 1, 2014
    Posts:
    4
    I have another problem with GUI Text. I'm trying to change a GUI Text using a script from another object whenever the function is used.

    This is the script that is trying to change the GUI Text:

    Code (JavaScript):
    1. var papercount = 0;
    2.  
    3. function OnTriggerEnter (other : Collider)
    4. {
    5.     Destroy(other.gameObject);
    6.     papercount = papercount + 1;
    7.     GameObject.Find("GUI Text").GetComponent(GUIText).Text = "Papers Collected: " + papercount;
    8.     GameObject.Find("body").GetComponent(Enemy_AIChase).MoveSpeed = GameObject.Find("body").GetComponent(Enemy_AIChase).MoveSpeed + 1;
    9. }
    Please help!
     
    Last edited: Aug 5, 2014
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    On a side note, use code tags. It makes helping you out easier. Just put the text "code" inside of brackets before your code and [/code] after to get it to look like this
    Code (csharp):
    1.  
    2. This is some code
    3.  
     
  6. Buklau123

    Buklau123

    Joined:
    Aug 1, 2014
    Posts:
    4
    Thanks for the tip! :)