Search Unity

Noob needs help getting his first script to work

Discussion in 'Scripting' started by VolkertJansz, Jan 15, 2020.

  1. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    Hi, I'm totally new with creating scripts in Unity. I'm trying to get a hinge motor to turn on when I press a OVR button. I'm getting " A namespace cannot directly contain members such as field or methods".

    Can anyone please help giving me some insight in what I'm doing wrong?

    Thanks in advance!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LeftWheelMotor : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.  
    11.         var hinge = GetComponent<HingeJoint>();
    12.  
    13.         // Make the hinge motor rotate with 90 degrees per second and a strong force.
    14.         var motor = hinge.motor;
    15.         motor.force = 100;
    16.         motor.targetVelocity = 90;
    17.         motor.freeSpin = false;
    18.         hinge.motor = motor;
    19.         hinge.useMotor = false;
    20.  
    21.     }
    22.  
    23. }
    24.  
    25. // Update is called once per frame
    26. void Update()
    27. {
    28.     if (OVRInput.GetDown(OVRInput.RawButton.Y))
    29.     {
    30.         hinge.useMotor = true;
    31.  
    32.     }
    33. }
     
  2. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    take the bracket off of line 23 and add one at line 34.
     
    VolkertJansz likes this.
  3. Deleted User

    Deleted User

    Guest

    Read your script again and see where you put Update(). Then put it back where it should be.
     
    VolkertJansz likes this.
  4. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    Yes! Thanks, that got rid of that error code.
    Though now it's telling me "the name 'hinge' does not exist in the current context"

    I thought the void Update would now replace the false for hinge.useMotor into a true when I press the button.
     
  5. Deleted User

    Deleted User

    Guest

    Is the script added to the object that contains the hinge?

    For clarity, you should replace "var" by the explicit types.
     
    VolkertJansz likes this.
  6. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    Yes, it is added to the wheel that contains the hinge. So how do I know the explicit type? I'm making this script for the left wheel, and i'm also going to make a script for the right wheel. So indeed I would need to specifiy which script affects which wheel. Just don't know how.
     
  7. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    I'm assuming I need to specify the name of the GameObject that contains the hinge?
     
  8. Deleted User

    Deleted User

    Guest

    Visual Studio can do that for you. Select the line that contains var and click on the small lamp bulb on the left.
     
    VolkertJansz likes this.
  9. Deleted User

    Deleted User

    Guest

    By the way, hinge is defined locally in Start and you use it in Update(). You need to make it global.
     
    VolkertJansz likes this.
  10. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    you've got to declare your variable before using it too. you're going to want to check up on some tutorials before you jump into just scripting like this. you're going to have hundreds of questions, and usually this forum is for when you're stuck at something but already have an understanding of the basics.

    watch this video series, https://www.youtube.com/playlist?list=PLX2vGYjWbI0S9-X2Q021GUtolTqbUBB9B

    and then this video series. https://www.youtube.com/playlist?list=PLX2vGYjWbI0S8YpPPKKvXZayCjkKj4bUP
     
    VolkertJansz likes this.
  11. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    Thanks APSchmidt! But no bulbs are being found and Ctrl+ isn't working

    Like Anthony mentions I might be better off going through some tutorials before I bother you guys.

    I'll go through the suggestions!
    Thanks a lot so far!
     
  12. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    So I got it to work. Most likely a horrible way of making it work, but I'm happy nonetheless. I also wanted the wheels to go in reverse when I pressed another button. This is what I ended up with:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RightWheelMotor : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Update()
    9.     {
    10.  
    11.         var hinge = GetComponent<HingeJoint>();
    12.  
    13.         if (OVRInput.Get(OVRInput.Button.Two))
    14.        
    15.         {
    16.             var motor = hinge.motor;
    17.             motor.force = 600;
    18.             motor.targetVelocity = -400;
    19.             motor.freeSpin = false;
    20.             hinge.motor = motor;
    21.             hinge.useMotor = true;
    22.         }
    23.  
    24.         else if (OVRInput.Get(OVRInput.Button.One))
    25.        
    26.         {
    27.             var motor = hinge.motor;
    28.             motor.force = 600;
    29.             motor.targetVelocity = 400;
    30.             motor.freeSpin = false;
    31.             hinge.motor = motor;
    32.             hinge.useMotor = true;
    33.         }
    34.         else
    35.         {
    36.             hinge.useMotor = false;
    37.         }
    38.  
    39.     }
    40. }
    41.  
    I'm sure there is some redundant stuff in there, and there might be better ways of doing it.
    I want to thank you guys for replying.

    Cheers
     
  13. KiddUniverse

    KiddUniverse

    Joined:
    Oct 13, 2016
    Posts:
    115
    declare your hinge outside of update and assign it in start so you're not using getcomponent every frame.
     
    VolkertJansz likes this.
  14. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    Yes, that makes a lot of sense :). Will do! Thanks!
     
    KiddUniverse likes this.