Search Unity

GUI and Raycast script help

Discussion in 'Scripting' started by Roninxfang, Sep 17, 2014.

  1. Roninxfang

    Roninxfang

    Joined:
    May 12, 2013
    Posts:
    75
    need help with some script

    good news is when i play it no errors and kind of got the logic how the script works

    bad news what i want it to do is now working

    long story short trying 2 simple things out

    1. for GUIText whatever stored value on a variable i have on the player script etc should display on the GUI text

    example in the player game object placed a script and declared a variable called "speed" with a value of 25...

    that i want to show in the GUI Text

    from what i understand in order to get value from a variable from another script i should use the gameObject.component

    but i kinda of works if its a component of a game object...

    but what if i am trying to access a value from a variable from another script?

    anyways made two scripts one for the player

    http://pastebin.com/Q2Qbxcwr

    and one for the GUI

    http://pastebin.com/DXuwW20d


    2. this next set of script...basically trying out raycasting to check distance between to objects

    basically what i wanted to simply happen if player is close enough to a certain object a debug.log ("your too close") will appear...


    http://pastebin.com/GsaEMsiC




    the good news is when i hit play no errors BUT the things i want to happen...cant seem to get it working hmmm
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    This is how you access scriptA from scriptB;

    //scriptB - C#
    scriptA component = gameObject.GetComponent<scriptA>();
    //scriptB - JS
    var component : scriptA = gameObject.GetComponent("scriptA");

    Hope this helps! :)
     
  3. Roninxfang

    Roninxfang

    Joined:
    May 12, 2013
    Posts:
    75
    hmmm so thats accessing from another script...but what how do type in javascript accessing specific value of a variable from another script?
     
  4. Roninxfang

    Roninxfang

    Joined:
    May 12, 2013
    Posts:
    75
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    Well once you have the reference to your script
    you'd do something like:

    (assuming code like the code I posted above)
    component.theScriptVariable = blah;
    or
    var blah = component.theScriptVariable;

    Hope this makes sense! :)
     
  6. Roninxfang

    Roninxfang

    Joined:
    May 12, 2013
    Posts:
    75
    hmmm cant seem to get it working hmmm

    heres the two scripts

    On the Box
    name of the game object is Cube, name of the script is Box

    #pragma strict
    var speed : int ;

    function Start () {

    }

    function Update () {
    speed = 25 ;
    }

    On the GUI Text
    name of the GUIText is Speed Value, name of the script is AccessData


    #pragma strict
    var SpeedValue : GUIText ;
    var Box : GameObject ;

    function Start () {

    }

    function Update () {

    GUIText.text = gameobject.find("Cube").getcomponent("Box").speed;



    }