c# - Component.GetComponent<Rigidbody>() causing a ton of errors -


i trying follow space shooter tutorial online unity5 , having trouble rigidbody.

i realize rigidbody has been replaced component.getcomponent() want make variable instead of typing out.

i getting ton of errors using component.getcomponent() , dont understand what's wrong.

here code snippet, trying constrain movement clamp:

using unityengine; using system.collections;  public class playercontroller : monobehaviour {      public float speed;     public float xmin, zmin, xmax, zmax;      void fixedupdate(){         float movehorizontal = input.getaxis("horizontal");         float movevertical = input.getaxis("vertical");          vector3 movement = new vector3(movehorizontal, 0.0f, movevertical);         component.getcomponent<rigidbody>().velocity = movement*speed;          component.getcomponent<rigidbody>().position = new vector3         (             mathf.clamp(component.getcomponent<rigidbody>().position.x, xmin, xmax),             0.0f,             mathf.clamp(component.getcomponent<rigidbody>().position.z, zmin, zmax)         );     } } 

and here spitload of errors gives me:

finished updating scripts / assemblies  scripts have compilation errors may prevent obsolete api usages updated. obsolete api updating continue automatically after these errors fixed.  assets/scripts/playercontroller.cs(14,27): error cs0120: object reference required access non-static member `unityengine.component.getcomponent(system.type)'  assets/scripts/playercontroller.cs(14,47): error cs0120: object reference required access non-static member `unityengine.component.getcomponent(system.type)'  assets/scripts/playercontroller.cs(18,31): error cs1502: best overloaded method match `unityengine.mathf.clamp(float, float, float)' has invalid arguments  assets/scripts/playercontroller.cs(18,31): error cs1503: argument `#1' cannot convert `object' expression type `float'  assets/scripts/playercontroller.cs(20,47): error cs0120: object reference required access non-static member `unityengine.component.getcomponent(system.type)'  assets/scripts/playercontroller.cs(20,31): error cs1502: best overloaded method match `unityengine.mathf.clamp(float, float, float)' has invalid arguments  assets/scripts/playercontroller.cs(20,31): error cs1503: argument `#1' cannot convert `object' expression type `float'  assets/scripts/playercontroller.cs(21,18): error cs1502: best overloaded method match `unityengine.vector3.vector3(float, float, float)' has invalid arguments  assets/scripts/playercontroller.cs(21,18): error cs1503: argument `#1' cannot convert `object' expression type `float'  assets/scripts/playercontroller.cs(16,27): error cs0120: object reference required access non-static member `unityengine.component.getcomponent(system.type)' 

i feel im missing important , obvious because isn't lot of code warrant many errors.

you should first create instance of object before using non-static class functions.

in case gameobject have rigidbody component attached. here code example:

gameobject.getcomponent<rigidbody>().velocity = movement * speed; 

redo other strings in code respectively.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -