Search Unity

lame question

Discussion in 'Scripting' started by rasq, Jun 10, 2009.

  1. rasq

    rasq

    Joined:
    May 25, 2009
    Posts:
    51
    hi, i have some problem, i am new in JS, and other scripting languages, i have problem:

    1. i have script: script1.js, on this scrip i have function:

    Code (csharp):
    1. function Update() {
    2. if (minitab.minitab == false)                                                  
    3.             {
    4.             script2.skaluj(0.5202637, 0.142, 1, 0.01);
    5.             } if (minitab.minitab == true)
    6.                     {
    7.                     script2.skaluj(0.5202637, 0.1259766, 1, 0.04);
    8.                     }  
    9. }
    this function reffer to function on script2

    2. on script2 i have:
    Code (csharp):
    1.  
    2. static function skaluj(x : int, y : int, z : int, sy : int) {
    3.                     object.transform.localScale.y = sy;
    4.                     object.transform.position = Vector3 (x, y, z);
    5. }
    3. and i have error:
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. script2.skaluj (Int32 x, Int32 y, Int32 z, Int32 sy)   (at Assets\skrypty\script2.js:34)
    4. pelenekran.Update ()   (at Assets\skrypty\przyciski\menu\pelenekran.js:42)
    4. script1 is attachet to GUITexture, i like to transform this texture by script2 called by script1.


    who can help me?? plese and thank. :]
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    What is "object" referring to in script2.js? I assume it's supposed to refer to the original game object (your GUITexture), if so then you need to pass a reference to that object when calling the function. For example:

    Code (csharp):
    1. // script1
    2. function Update() {
    3.   if (minitab.minitab == false) {
    4.     script2.skaluj(gameObject, 0.5202637, 0.142, 1, 0.01);
    5.   } if (minitab.minitab == true) {
    6.     script2.skaluj(gameObject, 0.5202637, 0.1259766, 1, 0.04);
    7.   }    
    8. }
    Code (csharp):
    1. // script2
    2. static function skaluj(object : GameObject, x : int, y : int, z : int, sy : int) {
    3.   object.transform.localScale.y = sy;
    4.   object.transform.position = Vector3 (x, y, z);
    5. }

    As your code was provided there is no valid reference assigned to the variable 'object' in script2.js and thus the error. Give that a go and see how it works!
     
  3. rasq

    rasq

    Joined:
    May 25, 2009
    Posts:
    51
    i dont know what i done bad (maybe this is problem of time 2p.m) but this dont work...

    no exacly dont work, but now my GUITexture disapeard :/

    i dont have any errors...

    i was copy
    Code (csharp):
    1. object : GameObject
    and
    Code (csharp):
    1. gameObject
    into my code...

    if i done somthing bad please tell me.

    i will be back at the morrning :p
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    While you might think your animation values are "sensible", are they really sensible? Your GUITexture may be disappearing as it's animating, quickly, right out of view. Select the GUITexture object in the hierarchy and hit play, watch the values. Does it animate the numbers? If so then things are "working" and you need to adjust the amounts used (likely with much smaller values).
     
  5. rasq

    rasq

    Joined:
    May 25, 2009
    Posts:
    51
    hmmm...



    i dont know. this is somthink like: gameObject doesent, sent to script name of this gameobjects, only string: gameObject.


    no, this is not this problem. because values are not change.


    i dont know maybe is another way to have name of GUITexture, who call a function???
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    A few thoughts, some of which I should have mentioned before:

    - Don't set the localScale for the GUITexture's game object, if you want to resize it then adjust the pixel inset rect value instead.

    - When you call skaluj() you provide floats, yet in that function you specify them as int's, you might want to clarify which you hope to use.

    - Commenting out the scale line and the if conditional in script1 makes your scripts work, as they are, with no issues. When I press play the GUITexture object jumps to the specified position (0.5202637,0.142). Nothing disappears for me, so I'm wondering if that is a complication due to your scaling attempts via localScale.


    Please test this in a bare bones simple new project (one just to sort this out). I sense that there are other factors at play given the line numbers cited in your initial report above (error on line 32 of script2, yet there are nowhere near 32 lines shown here :) ). This code works in general though, just trim out the localScale setting and your float/int type declarations and I think you should have it sorted.
     
  7. rasq

    rasq

    Joined:
    May 25, 2009
    Posts:
    51
    ok, i will try :] thanks very much :]

    and i will write heare other questions or sumary, what helps me :]

    ---

    o my good, i am so stupid, and blind :p

    you have right, using float when i declare int, are wery sdtupid...

    but more stupis is using int, where i must us float :]

    when i change inf to float, everything start to by ok.


    thanks very much :]