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. Dismiss Notice

Adjusting C# variable with Javascript

Discussion in 'Scripting' started by rmele09, Mar 14, 2015.

  1. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    As you may have guessed, I program in javascript (unityscript). I have a character that uses the Unity 5 first person controller, which is a C# script. I need to temporarily change the characters runspeed on this script, I would normally do this by changing the variable with something like I outlined below. I also need to figure out the C# syntax for GetComponent to send the variable changes back to the javascript file, after the "effect wears off". However, the variable is displayed very differently in the C# script than it would be in javascript, it looks like this:

    [SerializeField] private float m_RunSpeed;
    (This appears as a public var in the inspector although it says private)


    Javascript code I would usually use to change variable:

    var playerSpeed = GameObject.FindWithTag("Player").transform;
    playerSpeed.GetComponent.<Player>().speed = 5;
     
  2. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    I changed the C# variable to:
    public float m_RunSpeed;

    I guess my question is how can I use this technique of javascript:
    var playerSpeed = GameObject.FindWithTag("Player").transform;
    playerSpeed.GetComponent.<Player>().speed = 5;

    to access that variable? I am thinking that the syntax would need to change?
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Code (JavaScript):
    1. var myVar : YourType  = GetComponent(YourType);
    or find the player first, and use player.GetComponent if they're not on the same gameobject.

    You also have to make sure that the C# scripts that you want to access from US scripts are (for example) in the Plugins folder. However, if they're still in the Standard Assets folder, that should already be fine. Just make sure that the US script is not in the same folder.

    *Edit One more thing, you might need to either import the namespace or have the fullqualified name as the type.
     
  4. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    Hey thanks for the example. Could I ask you to be a little more specific, I have never touched c# and your code is confusing me. How do I use what you just wrote with the javascript method I wrote?
     
    Last edited: Mar 15, 2015
  5. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    This is what I tried:
    var playerSpeed = GameObject.FindWithTag("Player").transform;
    playerSpeed.GetComponent.<UnityStandardAssets.Characters.FirstPerson>().m_RunSpeed = 5;

    AND

    var playerSpeed = GameObject.FindWithTag("Player").transform;
    playerSpeed.GetComponent.<FirstPersonController>().m_RunSpeed = 5;

    Both basically give me this same error:
    The name 'UnityStandardAssets.Characters.FirstPerson' does not denote a valid type ('not found'). Did you mean 'UnityStandardAssets.Characters.FirstPerson.FirstPersonController'?
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The code sample above was UnityScript, C# looks different.

    If you want to use the type without an import you have to use 'UnityStandardAssets.Characters.FirstPerson.FirstPersonController' as a type.
    But that's too long, so put the following somwhere after #pragma strict
    Code (JavaScript):
    1. import UnityStandardAssets.Characters.FirstPerson;
    Then you should be able to use the first code piece that you posted.
     
  7. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    688
    Ahh yet again you are the man. Works perfect!