- Home /
transform.position assign attempt for 'Cubie' is not valid. Input position is { NaN, NaN, NaN }.
So here's some background information, I am trying to create this explosion effect of "cubie's". I have one object called enemy that when he collides with a bullet it destroys itself and instantiates another object called EnemyDeathAnimation which is not really an animation but a gameObject that replicates an explosion effect based on this tutorial: http://gamedevelopment.tutsplus.com/tutorials/how-to-make-an-object-shatter-into-smaller-fragments-in-unity--gamedev-11795
when I play the game and shoot the enemies occasionally I get the NaN error. I think it may be caused by the sphere used in the explosion effect instantiating and interfering with other objects. If anyone could shed some light on how to avoid/fix the error that would be great. If there is any better way of creating the explosion effect I would be more than happy to scratch this method and do it that way.
Enemy Script: using UnityEngine; using System.Collections;
public class EnemyCubeScript : MonoBehaviour
{
public GameObject deathAnimation;
private float fadeTimer = 1.9f;
private Transform player;
private NavMeshAgent nav;
void OnTriggerEnter (Collider col)
{
if(col.tag == "Bullet")
{
GameObject deathAnimationClone = Instantiate(deathAnimation, transform.position, transform.rotation) as GameObject; //instantiates deathAnimation gameObject
Destroy (gameObject); //destroy self
Destroy (deathAnimationClone, fadeTimer);
Destroy (col.gameObject); //destroy bullet
}
}
void Awake () {
player = GameObject.FindGameObjectWithTag ("Player").transform;
nav = GetComponent <NavMeshAgent> ();
}
void Update () {
nav.SetDestination (player.position);
}
}
I'm having a hard time understanding why the error occurs what appears to be at random. Also I'm confused as to why ending this game object and script would result in an error at all.
There are quite a few reasons for NaN. You need to look hard at the link (and other info) and try to find any places in your scripts in which those things may occur.
If in any script you ever divide anything by anything else, find out if that anything else could be 0 at some point. Good starting place. Search google for "Unity answers NaN".
Answer by meat5000 · Nov 28, 2014 at 04:47 PM
if(col.tag == "Bullet")
{
GameObject deathAnimationClone = Instantiate(deathAnimation, transform.position, transform.rotation) as GameObject; //instantiates deathAnimation gameObject
Destroy (gameObject); //destroy self
BANG! End of script... and gameobject.
http://en.wikipedia.org/wiki/NaN - Look under 'Operations generating NaN'. Divide by zero seems to be quite a common one.
It can also represent missing information, which is why I point out the gameobject destruction, as a case where info could be missing.
Also make sure the is information in all your variables. Its a safe practise to initialise them when you declare them.
Answer by CCV334 · Nov 29, 2014 at 03:05 AM
11/29/14
It appears that if I remove the sphere causing the "cubies" to explode and changing its transform fixes the issue. It is not exactly the effect I was going for but will suffice for now.
==========================================================================================
12/11/14
I found that by by experimenting with the Unity particle system I was able to create the effect I was looking for and found this tutorial specifically useful: http://youtu.be/Tg4bgOR2HDM?t=17m49s