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

Moving Player back 2 spaces

Discussion in 'Getting Started' started by DocJ, Apr 15, 2020.

  1. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    I had it! I freaking had it working! Then I started fiddling around with the code and I think I totally fried my brain cells. Should have taken a screen shot of the code while it was working!

    Please help straighten this back out........

    I'm trying to get my player to move back 2 spaces when he lands on a penalty space.
    1. Number gets generated
    2. Number is passed on to Player script
    3. Player is moved to appropriate space
    4. Check space to see if it's a hazard space.
    5. If it is, move it back 2 spaces.

    Everything works fine 1-3. Now I'm getting a Null reference Exception:Object reference not set to an instance of an object error (Line 49)

    I'm getting the script correctly announce the sentence from the Debug.Log and to subtract 2 from the movement total, but the Player is not moving. And it was, for a brief moment in time, it was!

    routePosition is an integer in the Player script which holds the index of the List of transforms.


    Thank you for any help getting this back on track!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LandingSpaces : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public TargetPath targetPathScript;
    10.     public Player playerScript;
    11.  
    12.  
    13.     GameObject bluePlayer;
    14.  
    15.    
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         playerScript = GameObject.Find("Blue Player").GetComponent<Player>();
    21.  
    22.         bluePlayer = GameObject.Find("Blue Player");
    23.  
    24.         Vector3 hazardPos = (targetPathScript.targetList[playerScript.routePosition - 2].position);
    25.  
    26.         transform.position = hazardPos;
    27.  
    28.  
    29.  
    30.  
    31.     }
    32.  
    33.  
    34.     public void WhatSpace()
    35.     {
    36.         if (playerScript.routePosition >= 21)
    37.         {
    38.             Debug.Log(" OUT OF BOUNDS!!");
    39.         }
    40.  
    41.         else if ((playerScript.routePosition == 6) || (playerScript.routePosition == 7))
    42.         {
    43.             Debug.Log(playerScript.routePosition +" You landed in the Ocean! ");
    44.  
    45.             playerScript.routePosition =  playerScript.routePosition - 2;
    46.  
    47.             Vector3 hazardPos = (targetPathScript.targetList[playerScript.routePosition - 2].position);
    48.  
    49.             bluePlayer.transform.position = hazardPos;
    50.  
    51.         }
    52.           // etc, etc.......more of the same
    53.     }
    54. }
     
  2. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Perhaps you could try to log all of your variables, to see which one is missing, and fix it from there.
     
  3. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Ok I see some issues but since you didn't supply all related scripts I'm guessing.

    Kind of thinking you should be combining your Player script and your LandingSpaces into one script.

    Your void Start() can be simplified if you do. Also Start is executed once after the GameObject is initialized, there should be no hazardPos when routPosition is zero. Zero - 2 is not a legal index.

    Next, in void WhatSpace() you are basically saying if routePosition is less than 21 and equal to 6 or 7 then then move player object. Any other value of routePosition will do nothing.

    My suggestion is to write down what you want to happen in pseudo code then convert that to actual code. Sometimes it's hard to work out the logic needed while actually writing and debugging real code.
     
    DocJ likes this.