1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Access variable from other gameobject

Discussion in 'Scripting' started by fmaddict, Jul 10, 2011.

  1. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    how would i access a variable from a script on a different game object?
    The variables are either booleans or objects set in unity with a Transform in script
    I'm using java
     
  2. Stec

    Stec

    Joined:
    Apr 13, 2011
    Posts:
    118
  3. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    These seem to invoke functions rather than access variables. They state do not work with my booleans when i try to use it
     
  4. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,832
    Make sure your booleans are public.

    Because the docs are correct (and work with accessing variables).
     
  5. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    there definitely public variables.

    the error that I am getting when its trying to compile the script is "It is not possible to invoke an expression of type 'boolean'."
     
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,832
    If you have code that doesn't work, first step is to show us.
     
  7. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    I'm not quite sure which scripts i should be posting but here is the little bit i have to try and get it working before implement it into my main one

    Code (csharp):
    1. var script : gui;
    2. script = GetComponent("gui");
    3. script.sw1 ();
    trying to access these variables from a script called 'gui'

    Code (csharp):
    1. var sw1 : boolean = true;
    2. var sw2 : boolean = true;
    3. var sw3 : boolean = true;
    4. var sw4 : boolean = true;
     
  8. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,832
    As I thought.

    Code (csharp):
    1.  
    2. script.sw1 ();
    3.  
    What are the brackets there for? Are you calling some function? This is what the error message was saying:

    What you should be doing is using it as you would any other boolean:

    Code (csharp):
    1.  
    2. //set to false
    3. script.sw1 = false;
    4.  
    5. // toggle
    6. script.sw1 = !script.sw1;
    7.  
    8. //If statement
    9.  
    10. if (script.sw1) //do something
    11.  
    12. //make sure it works
    13. debug.log(script.sw1);
    14.  
    15. //etc.
    16.  
     
  9. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    ok thank you very much that fixed it. I'm sure i tried the if statement with that and same problem but ohh well.

    i am very grateful for your help.
     
  10. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    ok 1 day on and i have problems again.

    when testing with this script it works fine
    Code (csharp):
    1. var script : path1;
    2. script = GetComponent("path1");
    3.  
    4. function Update(){
    5.     Debug.Log(script.w1);
    but when i put it in my full script it doesnt work anymore. With the error
    "NullReferenceException: Object reference not set to an instance of an object
    mover.Start () (at Assets/Movement/mover.js:15)"

    Code (csharp):
    1. var waypoint = new Array();
    2. private var currentWaypoint : int;
    3.  
    4. var speed : float = 10;
    5. var loop : boolean = false;
    6.  
    7. var ways : path1;
    8. ways = GetComponent("path1");
    9. var gui : gui1;
    10. gui = GetComponent("gui1");
    11.  
    12.  
    13.  
    14. function Start(){
    15.     waypoint.Add(ways.w7);
    16.     waypoint.Add(ways.w8);
    17.     waypoint.Add(ways.w9);
    18.     waypoint.Add(ways.w10);
    19.     waypoint.Add(ways.w82);
    20.     waypoint.Add(ways.w83);
    21.     Debug.Log(gui.sw1);
    22.     Debug.Log(waypoint);
    23. }
    24.  
    25.  
    26. function Update(){
    27.  
    28.     if(currentWaypoint < waypoint.length){
    29.         var target : Vector3 = waypoint[currentWaypoint].position;
    30.         var moveDirection : Vector3 = target - transform.position;
    31.        
    32.         var velocity = rigidbody.velocity;
    33.        
    34.         if(moveDirection.magnitude < 1){
    35.             currentWaypoint++;
    36.         }
    37.         else{
    38.             velocity = moveDirection.normalized * speed;
    39.         }
    40.     }
    41.     else{
    42.         if(loop){
    43.             currentWaypoint=0;
    44.         }
    45.         else{
    46.             velocity = Vector3.zero;
    47.         }
    48.        
    49.     }
    50.    
    51.     rigidbody.velocity = velocity;
    52.     transform.LookAt(target);
    53. }
    54.  
    55.  
    56. function OnTriggerEnter(collision : Collider){
    57.     if (collision == ways.w83){
    58.         if(gui.sw1){
    59.             waypoint.Add(ways.w84);
    60.             waypoint.Add(ways.w85);
    61.             waypoint.Add(ways.w86);
    62.             waypoint.Add(ways.w87);
    63.             waypoint.Add(ways.w88);
    64.             waypoint.Add(ways.w89);
    65.             waypoint.Add(ways.w90);
    66.             waypoint.Add(ways.w91);
    67.             waypoint.Add(ways.w92);
    68.             waypoint.Add(ways.w93);
    69.             waypoint.Add(ways.w94);
    70.             waypoint.Add(ways.w95);
    71.             waypoint.Add(ways.w109);
    72.             waypoint.Add(ways.w110);
    73.         }
    74.         else{
    75.             waypoint.Add(ways.w104);
    76.         }
    77.     }
    78.    
    79.     if (collision == ways.w104){
    80.         if(gui.sw2){
    81.             waypoint.Add(ways.w106);
    82.             waypoint.Add(ways.w116);
    83.         }
    84.         else{
    85.             waypoint.Add(ways.w105);
    86.             waypoint.Add(ways.w107);
    87.         }
    88.     }
    89.    
    90.         if (collision == ways.w116){
    91.         if(gui.sw3){
    92.             waypoint.Add(ways.w118);
    93.             waypoint.Add(ways.w120);
    94.             waypoint.Add(ways.w108);
    95.             waypoint.Add(ways.w109);
    96.             waypoint.Add(ways.w110);
    97.         }
    98.         else{
    99.             waypoint.Add(ways.w117);
    100.             waypoint.Add(ways.w119);
    101.             waypoint.Add(ways.w101);
    102.         }
    103.     }
    104.    
    105.         if (collision == ways.w101){
    106.         if(gui.sw4){
    107.             waypoint.Add(ways.w80);
    108.             waypoint.Add(ways.w12);
    109.             waypoint.Add(ways.w122);
    110.         }
    111.         else{
    112.             waypoint.Add(ways.w102);
    113.             waypoint.Add(ways.w103);
    114.             waypoint.Add(ways.w108);
    115.             waypoint.Add(ways.w109);
    116.             waypoint.Add(ways.w110);
    117.         }
    118.     }
    119.    
    120.         if (collision == ways.w96){
    121.         if(gui.sw5){
    122.             waypoint.Add(ways.w97);
    123.             waypoint.Add(ways.w78);
    124.             waypoint.Add(ways.w66);
    125.         }
    126.         else{
    127.             waypoint.Add(ways.w98);
    128.             waypoint.Add(ways.w99);
    129.             waypoint.Add(ways.w100);
    130.             waypoint.Add(ways.w116);
    131.         }
    132.     }
    133.    
    134.         if (collision == ways.w110){
    135.         if(gui.sw6){
    136.             waypoint.Add(ways.w112);
    137.             waypoint.Add(ways.w114);
    138.             waypoint.Add(ways.w13);
    139.             waypoint.Add(ways.w122);
    140.         }
    141.         else{
    142.             waypoint.Add(ways.w111);
    143.             waypoint.Add(ways.w113);
    144.             waypoint.Add(ways.w115);
    145.             waypoint.Add(ways.w81);
    146.             waypoint.Add(ways.w12);
    147.             waypoint.Add(ways.w122);
    148.         }
    149.     }
    150.    
    151.         if (collision == ways.w66){
    152.         if(gui.sw7){
    153.             waypoint.Add(ways.w80);
    154.             waypoint.Add(ways.w12);
    155.             waypoint.Add(ways.w122);
    156.         }
    157.         else{
    158.             waypoint.Add(ways.w67);
    159.             waypoint.Add(ways.w68);
    160.             waypoint.Add(ways.w69);
    161.             waypoint.Add(ways.w50);
    162.             waypoint.Add(ways.w51);
    163.         }
    164.     }
    165.    
    166.         if (collision == ways.w51){
    167.         if(gui.sw8){
    168.             waypoint.Add(ways.w53);
    169.             waypoint.Add(ways.w55);
    170.             waypoint.Add(ways.w70);
    171.             waypoint.Add(ways.w12);
    172.             waypoint.Add(ways.w122);
    173.         }
    174.         else{
    175.             waypoint.Add(ways.w52);
    176.             waypoint.Add(ways.w54);
    177.             waypoint.Add(ways.w56);
    178.             waypoint.Add(ways.w57);
    179.             waypoint.Add(ways.w58);
    180.             waypoint.Add(ways.w11);
    181.             waypoint.Add(ways.w122);
    182.         }
    183.     }
    184.    
    185.         if (collision == ways.w14){
    186.         if(gui.sw9){
    187.             waypoint.Add(ways.w31);
    188.             waypoint.Add(ways.w32);
    189.         }
    190.         else{
    191.             waypoint.Add(ways.w15);
    192.             waypoint.Add(ways.w16);
    193.             waypoint.Add(ways.w17);
    194.             waypoint.Add(ways.w18);
    195.             waypoint.Add(ways.w18);
    196.             waypoint.Add(ways.w20);
    197.         }
    198.     }
    199.    
    200.         if (collision == ways.w32){
    201.         if(gui.sw10){
    202.             waypoint.Add(ways.w33);
    203.             waypoint.Add(ways.w35);
    204.             waypoint.Add(ways.w37);
    205.             waypoint.Add(ways.w39);
    206.             waypoint.Add(ways.w41);
    207.             waypoint.Add(ways.w44);
    208.             waypoint.Add(ways.w45);
    209.         }
    210.         else{
    211.             waypoint.Add(ways.w34);
    212.             waypoint.Add(ways.w36);
    213.             waypoint.Add(ways.w38);
    214.             waypoint.Add(ways.w40);
    215.             waypoint.Add(ways.w42);
    216.             waypoint.Add(ways.w43);
    217.             waypoint.Add(ways.w44);
    218.             waypoint.Add(ways.w45);
    219.         }
    220.     }
    221.    
    222.         if (collision == ways.w20){
    223.         if(gui.sw11){
    224.             waypoint.Add(ways.w22);
    225.             waypoint.Add(ways.w23);
    226.             waypoint.Add(ways.w24);
    227.             waypoint.Add(ways.w65);
    228.             waypoint.Add(ways.w78);
    229.         }
    230.         else{
    231.             waypoint.Add(ways.w21);
    232.             waypoint.Add(ways.w25);
    233.         }
    234.     }
    235.    
    236.         if (collision == ways.w25){
    237.         if(gui.sw12){
    238.             waypoint.Add(ways.w26);
    239.             waypoint.Add(ways.w28);
    240.             waypoint.Add(ways.w31);
    241.             waypoint.Add(ways.w32);
    242.         }
    243.         else{
    244.             waypoint.Add(ways.w27);
    245.             waypoint.Add(ways.w29);
    246.             waypoint.Add(ways.w30);
    247.             waypoint.Add(ways.w48);
    248.             waypoint.Add(ways.w49);
    249.             waypoint.Add(ways.w50);
    250.             waypoint.Add(ways.w51);
    251.         }
    252.     }
    253.    
    254.         if (collision == ways.w71){
    255.         if(gui.sw13){
    256.             waypoint.Add(ways.w59);
    257.         }
    258.         else{
    259.             waypoint.Add(ways.w72);
    260.             waypoint.Add(ways.w73);
    261.             waypoint.Add(ways.w74);
    262.             waypoint.Add(ways.w75);
    263.             waypoint.Add(ways.w76);
    264.             waypoint.Add(ways.w82);
    265.             waypoint.Add(ways.w83);
    266.         }
    267.     }
    268.    
    269.         if (collision == ways.w59){
    270.         if(gui.sw14){
    271.             waypoint.Add(ways.w77);
    272.             waypoint.Add(ways.w78);
    273.         }
    274.         else{
    275.             waypoint.Add(ways.w60);
    276.             waypoint.Add(ways.w61);
    277.             waypoint.Add(ways.w62);
    278.             waypoint.Add(ways.w63);
    279.             waypoint.Add(ways.w64);
    280.             waypoint.Add(ways.w19);
    281.             waypoint.Add(ways.w20);
    282.         }
    283.     }
    284.    
    285.         if (collision == ways.w45){
    286.         if(gui.sw15){
    287.             waypoint.Add(ways.w56);
    288.             waypoint.Add(ways.w57);
    289.             waypoint.Add(ways.w58);
    290.             waypoint.Add(ways.w11);
    291.             waypoint.Add(ways.w122);
    292.         }
    293.         else{
    294.             waypoint.Add(ways.w46);
    295.             waypoint.Add(ways.w47);
    296.             waypoint.Add(ways.w48);
    297.             waypoint.Add(ways.w49);
    298.             waypoint.Add(ways.w50);
    299.             waypoint.Add(ways.w51);
    300.         }
    301.     }
    302.    
    303.         if (collision == ways.w78){
    304.         if(gui.sw16){
    305.             waypoint.Add(ways.w66);
    306.         }
    307.         else{
    308.             waypoint.Add(ways.w79);
    309.             waypoint.Add(ways.w101);
    310.         }
    311.     }
    312.    
    313.         if (collision == ways.w77){
    314.         if(gui.sw17){
    315.             waypoint.Add(ways.w78);
    316.         }
    317.         else{
    318.             waypoint.Add(ways.w96);
    319.         }
    320.     }
    321. }
     
  11. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    303
    replace with this:
    PHP:
    var ways;
    ways GetComponent("path1");
    var 
    gui;
    gui GetComponent("gui1");
    let unity decide what type is the variable
    a name of a script is not a type.
     
  12. fmaddict

    fmaddict

    Joined:
    Apr 2, 2011
    Posts:
    18
    i managed to fix it by adding gameObject.Find to force it to look in the right place. i believe it was finding the raw script with nothing attached rather than the one on the object.