Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Need help with debug new to coding

Discussion in 'Editor & General Support' started by AdamNowo, Dec 4, 2018.

  1. AdamNowo

    AdamNowo

    Joined:
    Dec 4, 2018
    Posts:
    2
    PlayerStateListener


    // Every cycle of the engine, process the current state.
    void onStateCycle()
    {
    switch (currentState)
    {
    case PlayerStateController.playerStates.idle:
    playeranimator.setbool("walking, false");
    break;

    case PlayerStateController.playerStates.left:
    playeranimator.setbool("walking", true);
    break;

    case PlayerStateController.playerStates.right:
    playeranimator.setbool("walking", true);
    break;
    }
    }

    // onStateChange is called when we make a change to the player's state from anywhere within the game's code.
    public void onStateChange(PlayerStateController.playerStatesnewState)
    {
    // If the current state and the new state are the same, abort - no need to change to the state we're already in.

    if (newState == currentState)
    return;

    // Check if the current state is allowed to transition into // this state. If it's not, abort.
    if (!checkForValidStatePair(newState))
    return;

    // Having reached here, we now know that this state change is // allowed. So let's perform the necessary actions depending // on what the new state is.
    switch (newState)
    {
    case PlayerStateController.playerStates.idle:
    break;

    case PlayerStateController.playerStates.left:
    break;

    case PlayerStateController.playerStates.right:
    break;
    }

    // And finally, assign the new state to the player object
    currentState = newState;
    }
    // Compare the desired new state against the current, and see // if we are allowed to change to the new state. This is a
    // powerful system that ensures we only allow the actions to // occur that we want to occur.

    bool checkForValidStatePair(PlayerStateController.playerStatesnewState)
    {
    bool returnVal = false;
    }
    // Compare the current[…]”

    //Excerpt From: “Unity 2D Game Development.” iBooks.


    PlayerStateController


    using UnityEngine;
    using System.Collections;
    public class PlayerStateController : MonoBehaviour
    {
    public enum playerStates
    {
    idle = 0,
    left,
    right,
    jump,
    landing,
    falling,
    kill,
    resurrect
    }

    public delegate void playerStateHandler(PlayerStateController.playerStatesNewState);

    public static event playerStateHandlerOnStateChange;
    void LateUpdate()
    {
    // Detect the current input of the Horizontal axis, then
    // broadcast a state update for the player as needed.
    // Do this on each frame to make sure the state is always
    // set properly based on the current user input.
    float horizontal = Input.GetAxis("Horizontal");
    if (horizontal != 0f)
    {
    if (horizontal < 0f)
    {
    if (onStateChange != null) onStateChange(PlayerStateController.playerStates.left);
    }
    else
    {
    if (onStateChange != null) onStateChange(PlayerStateController.playerStates.right);
    }
    }
    else
    {
    if (onStateChange != null) onStateChange(PlayerStateController.playerStates.idle);
    }
    }
    }

    //Excerpt From: “Unity 2D Game Development.” iBooks.
     
    Last edited: Dec 6, 2018
  2. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,927
    Please put your code among code tags, you can find the button in the toolbar when you're editing your post. You can edit your post clicking on the "edit" word at the bottom of your post.

    I'm not sure, because your code is a mess in this form, but this line should be replaced with this one:
    Code (CSharp):
    1. public void onStateChange(PlayerStateController.playerStates newState)
    .
    Code (CSharp):
    1. bool checkForValidStatePair(PlayerStateController.playerStates newState)
    .
    Code (CSharp):
    1. public delegate void playerStateHandler(PlayerStateController.playerStates newState);
    .
     
  3. AdamNowo

    AdamNowo

    Joined:
    Dec 4, 2018
    Posts:
    2
    Code (CSharp):
    1. public static event playerStateHandlerOnStateChange; }
    The rest of the code is good but this line still says identifier expected
     
  4. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,927
    Check the } in that line, are you sure you need it? I advise you to look for a beginner tutorial on C# first, because clearly you have no idea how a C# code should even look like. You shouldn't work on advanced topics like events and such. Learn the basic syntax first.

    https://mva.microsoft.com/en-us/tra...solute-beginners-16169?l=Lvld4EQIC_2706218949
     
  5. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    2,804
    I suspect it is the same issue, missing a space