c# - Unity 5 Upgrade Made Script from Unity 4.6 stop work -
the code below worked fine while using unity 4.6
using unityengine; using system.collections; public class hadescontroller : monobehaviour { public float maxspeed = 10f; bool facingright = true; animator anim; bool grounded = false; public transform groundcheck; float groundradius = 0.2f; public layermask whatisground; public float jumpforce = 700f; void start () { anim = getcomponent<animator>(); } void fixedupdate () { grounded = physics2d.overlapcircle (groundcheck.position, groundradius, whatisground); anim.setbool ("ground", grounded); anim.setfloat ("vspeed", getcomponent<rigidbody2d>().velocity.y); if (!grounded) return; float move = input.getaxis ("horizontal"); anim.setfloat("speed", mathf.abs (move)); getcomponent<rigidbody2d>().velocity = new vector2 (move * maxspeed, getcomponent<rigidbody2d>().velocity.y); if (move > 0 &&!facingright) flip (); else if (move <0 && facingright) flip (); } void update() { if (grounded && input.getkeydown (keycode.space)) { anim.setbool("ground", false); getcomponent<rigidbody2d>().addforce(new vector2(0, jumpforce)); } } void flip() { facingright = !facingright; vector3 thescale = transform.localscale; thescale.x *= -1; transform.localscale = thescale; } } after upgraded unity 5 gave me error message: unassignedreferenceexception: variable groundcheck of hadescontroller has not been assigned. need assign groundcheck variable of hadescontroller script in inspector. unityengine.transform.get_position () (at c:/buildslave/unity/build/artifacts/generated/common/runtime/unityenginetransform.gen.cs:28) hadescontroller.fixedupdate () (at assets/scripts/hadescontroller.cs:21)
this straightforward error. need see part of exception:
you need assign groundcheck variable of hadescontroller script in inspector.
that is, transform assigned groundcheck somehow lost , groundcheck null. should reassign it. drag , drop assigned transform (or gameobject) groundcheck in inspector again.
add debug check before error line , should see if it's null or not:
debug.log("groundcheck null: " + (groundcheck == null));
Comments
Post a Comment