- Home /
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]
Answer by Berenger · Feb 27, 2012 at 11:48 PM
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.
Forgot to mention, don't mix up GUI (2D) with world space (3D) stuff, or Inputs. Do that in Update().
Your answer

Follow this Question
Related Questions
FPS SCRIPT ERROR PLEASE HELP 2 Answers
Help spawning an object relative to the local Z axis of an object 0 Answers
Help with firing projectiles out of turret 1 Answer
Navmesh error on instantiated navmesh agents 1 Answer
Aim to crosshair? 3 Answers