Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

New to C# and Unity, What is wrong with my scripts?

Discussion in 'Scripting' started by Eyonix, Feb 27, 2015.

  1. Eyonix

    Eyonix

    Joined:
    Feb 27, 2015
    Posts:
    3
    Hello I am attempting to follow a youtube tutorial [Unity Tutorial] Platform | Sidescroller 01 and I have checked over my code twice now and I think I am missing something small because my code looks similar to his but unfortunately I do not fully understand the code yet so I need help with it if possible I would appreciate it.

    Code (CSharp):
    1.   using UnityEngine;
    2.   using System.Collections;
    3.   public class PlayerPhysics : MonoBehaviour {
    4.  
    5.      public void Move(Vector2 moveAmount){
    6.        transform.Translate(moveAmount);
    7.   }
    8.   }

    **My Second cs file.**

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(PlayerPhysics))]
    4. public class PlayerController : MonoBehaviour {
    5.    public float Acceleration = 12;
    6.    public float Speed = 8;
    7.    private float CurrentSpeed;
    8.    private float TargetSpeed;
    9.    private Vector2 AmountToMove;
    10.    private PlayerPhysics playerPhysics;
    11.    void Start () {
    12.      playerPhysics = GetComponent<PlayerPhysics>();
    13.    }
    14.    void Update () {
    15.      TargetSpeed = Input.GetAxisRaw ("Horizontal") * Speed;
    16.      CurrentSpeed = IncrementTowards (CurrentSpeed, TargetSpeed, Acceleration);
    17.      AmountToMove = new Vector2 (CurrentSpeed, 0);
    18.      playerPhysics.Move(AmountToMove * Time.deltaTime);
    19. }
    20.    private float IncrementTowards(float n, float Target, float a){
    21.      if (n == Target) {
    22.        return n;
    23.      } else {
    24.        float dir = Mathf.Sign (Target - n);
    25.        n += a * Time.deltaTime * dir;
    26.        return (dir == Mathf.Sign (Target - n)) ? n : Target;
    27.      }
    28.  
    29. }
    30.  
    Sorry as previously stated I have very little knowledge of C# maybe I am in over my head.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Please use code tags when posting code.

    Also you didn't mention if there was an error (if so, what is the error?), or if it isn't functioning as you expect (if so, what do you expect, and what is it doing?).
     
    JoeStrout likes this.
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Looks like you're missing a '}' at the end.
     
  4. Eyonix

    Eyonix

    Joined:
    Feb 27, 2015
    Posts:
    3
    Okay I fixed one of the errors I was receiving thank you Suddoha, I swear I checked that.

    The other error I am receiving in unity is
    it is supposed to be part of the movement script on this side scroller I'm making from the tutorial I'm following but I must have done something wrong because when he tests it the cube moves left and right. My unity will not let me test it because of the error.
     
    Last edited: Feb 28, 2015
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Well, that error message is saying that it can't find a function called "Move" inside of the class "PlayerPhysics".

    However, the first source file shown in your original post clearly defines a class called "PlayerPhysics" with a function called "Move", so my guess is that there's actually something wrong with your first file that is preventing Unity from finding it or compiling it.

    Is that the only error you're getting? Take a look in your console (you can open it from the Window menu) and check if there are any other errors.

    If you don't see any other errors, I guess check to make sure that your first file (the one defining PlayerPhysics) is showing up in your asset list.
     
    Kiwasi likes this.
  6. Eyonix

    Eyonix

    Joined:
    Feb 27, 2015
    Posts:
    3
    Just reopened my project today and nothing popped up when I clicked play but the controller did not function. however when I opened up MonoDevelop this popped up.

    Also both scripts are visible in my asset folder.