Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

inControl get info from c# script

Discussion in 'Scripting' started by 2dgamemania, Apr 5, 2018.

  1. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    HI I recently purchased inControl asset from the unity store. The code in the c# update does work ok

    if (playerActions.jump.WasPressed)
    {
    print("jumppressed");
    }

    My problem is, I need to use a javascript file to access the c# script in the same way.

    the below doesnt work

    if (otherredefinecontrols.playerActions.jump.WasPressed)
    {
    print("jumppressed");
    }

    It says Object reference not set to an instance of this object when I press the jump button.

    I have the redefinecontrols c# file on a object in the inspector and the object has been manually passed onto the redefineobject in the edtor. Could someone help, thanks

    #javascript file
    Code (csharp):
    1.  
    2. //javascript file
    3. var otherredefinecontrols : BindingsExamples.redefinecontrols;
    4. var redefineobject : GameObject;
    5.  
    6. function Start() {
    7.        otherredefinecontrols=redefineobject.GetComponent.<BindingsExamples.redefinecontrols>();
    8. }
    9.  
    10. function Update() {
    11.   if (otherredefinecontrols.playerActions.jump.WasPressed)
    12.         {
    13.                print("jumppressed");
    14.         }
    15. }
    16.  

    c# file (redefinecontrols)
    Code (csharp):
    1.  
    2. //c# file
    3. namespace BindingsExamples
    4. {
    5.     public class redefinecontrols : MonoBehaviour
    6.     {
    7.         public playerActions playerActions;
    8.  
    9.  
    10.         // Use this for initialization
    11.         void Start()
    12.         {
    13.             playerActions = new playerActions();
    14.             playerActions.jump.AddDefaultBinding(InputControlType.Action1);  //jump
    15.         }
    16.  
    17.  
    18.         void Update()
    19.         {
    20.              if (playerActions.jump.WasPressed)
    21.             {
    22.                 print("jumppressed");
    23.             }
    24.         }
    25. }
    26.  
     
  2. 2dgamemania

    2dgamemania

    Joined:
    Apr 30, 2014
    Posts:
    153
    I'm not exactly sure how i got this problem, but i had multiple copies for the same file, chose the other file and works now...