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

Roll a ball in JavaScript

Discussion in 'Scripting' started by Green_Ghost, Mar 10, 2015.

  1. Green_Ghost

    Green_Ghost

    Joined:
    Mar 10, 2015
    Posts:
    9
    I have just recently started a game development course at my school and we will be coding in JavaScript or unity-script for the creation of scripts for our games, so I was looking to become more familiar with this language. I started to create the roll a ball game in JavaScript and when I was creating the script to move the player I stumbled upon the problem of not being able to create a variable in the script that is able to be edited from the inspector menu like the speed variable in the video. If anyone could help me to remedy this problem it would be greatly appreciated as this will be a skill that I will most likely need in the future for other projects.
     
  2. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    Make sure the variable is public :)
    I use C# but in C# if you just put something like "float Test" it would be private, and if you put "public float Test" you will be able to edit in the inspector. I think it's the same in JS, I would google it :) Hope I helped, to me that seems to be your problem.
     
  3. Green_Ghost

    Green_Ghost

    Joined:
    Mar 10, 2015
    Posts:
    9
    Thank you for your comment but in JavaScript all variables are public by default so I don't think that this is the problem. I did however google it beforehand and I have yet to find a solution, but thank you for taking the time to read this post and try to help me out!
     
  4. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    no problem, good luck :)
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
    Can you show the script?
     
  6. Green_Ghost

    Green_Ghost

    Joined:
    Mar 10, 2015
    Posts:
    9
    This is what I have so far

    #pragma strict

    function Update () {

    var horizontal : float = Input.GetAxis("Horizontal");
    var vertical : float = Input.GetAxis("Vertical");
    var speed : float = 10;

    Debug.Log(horizontal);
    Debug.Log(vertical);

    rigidbody.AddForce(Vector3(horizontal, 0.0, vertical) * speed);

    }
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
    var speed : float = 10; needs to go outside the Update(), then it should display in the inspector
     
    Kiwasi likes this.
  8. Green_Ghost

    Green_Ghost

    Joined:
    Mar 10, 2015
    Posts:
    9
    Thank you mgear, I just changed this now and it works perfectly!
     
  9. peterkarki

    peterkarki

    Joined:
    Nov 19, 2016
    Posts:
    1
    Assets/scrip/my ball chater.js(12,11): BCE0019: 'AddForce' is not a member of 'UnityEngine.Component'.
    need help plzzzz
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is almost always because of abusing UnityScripts dynamic typing. Solutions, in order of merit.
    1. Upgrade to C#
    2. Use pragma strict to force C# like typing
    3. Manually type your variables like in C#
    4. Learn the intricacies of UnityScripts dynamic typing.
    Actually on a second read through it's because this script is really old, and was built using the Unity 4 quick accessors. These no longer exist in Unity 5. That's the problem with necroing old threads, they quickly become out of date.

    Solutions, in order of merit
    1. Use C# and the latest version of the roll a ball tutorial
    2. Downgrade to 4.6
    3. Replace rigidbody with the appropriate GetComponent call
    TL;DR: Use C#!
     
  11. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Make your own thread, hijacking is against thread rules.