x


Gun Switching Error

i am trying to make a shop script for my first person shoot game. i switch weapons buy deleting the old weapon and instantiating a new one from a prefab. the weapon is suppose to auto position it's self according to variables in it's script, those variables are used in the aim script attached to the camera witch is a parent of the gun.

the system works fine except that i get this error when i switch weapons

transform.localPosition assign attempt for 'vzr8(Clone)' is not valid. Input localPosition is { -45.218674, -0.350000, NaN }.
UnityEngine.Transform:set_localPosition(Vector3)
aim:Update() (at Assets/scripts/player/gun/aim.js:33)

can any one tell me what it means and how to fix it. btw here is my shop script

private var toggle : boolean = false;
private var player : Transform;
private var skin: GUIStyle;
private var bg: Texture;

var guns : Transform[];

function Start(){
    player = gameObject.FindWithTag("Player").transform;
    skin = gameObject.FindWithTag("Master").GetComponent("master_gameInterface").skin;
    bg = gameObject.FindWithTag("Master").GetComponent("master_gameInterface").bg;
}

function OnGUI() {
    var dist = Vector3.Distance(player.position, transform.position);

    myStyle = new GUIStyle();
    myStyle.normal.textColor = Color.cyan;
    myStyle.font = skin.font;

    if(dist<5){
       if(Input.GetKey(KeyCode.E)){
         master_gameInterface.pause = true;
         toggle = true;
       }
       if(toggle==false)
         GUI.Label(Rect(Screen.width/2,Screen.height/2,200,200), "Press E to enter Shop", myStyle);
    }

    if(master_gameInterface.toggle)
       toggle = false;

    if(toggle){
    GUILayout.BeginArea(Rect(Screen.width/2-100,Screen.height/2-200,200,400), bg);
    GUILayout.BeginVertical();
       for(var i=0;i<guns.Length;i++){
         if(GUILayout.Button(guns[i].GetComponent("gun").name, skin)){
          if(player.GetChild(2).GetChild(0))
              Destroy(player.GetChild(2).GetChild(0).gameObject);
          var me = Instantiate(guns[i], player.position, Quaternion.identity);
          me.parent = player.GetChild(2);
          me.localPosition = player.GetChild(2).position;
          player.GetChild(2).GetComponent("aim").gun = me;
         }
       }

       GUILayout.Label("\n");
       if(GUILayout.Button("Done", skin)){
         master_gameInterface.pause = false;
         toggle = false;
       }
    GUILayout.EndVertical();
    GUILayout.EndArea();
    }
}

function deleteGun(){
    for (var child : Transform in transform) {
        child.position += Vector3.up * 10.0;
    }
}

[Edit by Berenger : code formatting]

more ▼

asked Feb 27 '12 at 03:31 PM

dick gravatar image

dick
16 7 7 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

A NaN is a result of an invalid calculation. It's happening in your player's script I guess, as it is it's transform which is the problem. The best would be to find out what is causing the NaN, but you can check float.IsNaN and change the value to a default one.

more ▼

answered Feb 27 '12 at 11:48 PM

Berenger gravatar image

Berenger
11.2k 12 19 54

Forgot to mention, don't mix up GUI (2D) with world space (3D) stuff, or Inputs. Do that in Update().

Feb 28 '12 at 12:09 AM Berenger
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2030
x1726
x1200
x104
x33

asked: Feb 27 '12 at 03:31 PM

Seen: 556 times

Last Updated: Feb 28 '12 at 12:09 AM