Search Unity

Problems with Create with Code Lesson 3.3 Don't Just Stand There

Discussion in 'Community Learning & Teaching' started by Ufreewoody, Jan 11, 2020.

  1. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     private Rigidbody playerRb;
    8.     private Animator playerAnim;
    9.     public float jumpForce = 10;
    10.     public float gravityModifier;
    11.     public bool isOnGround = true;
    12.     public bool gameOver = false;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         playerRb = GetComponent<Rigidbody>();
    18.         playerAnim = GetComponent<Animator>();
    19.         Physics.gravity *= gravityModifier;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
    26.         {
    27.             // The Traing Video Code shows this line with a "10" before jumpForce
    28.             // whenever I try and add the "10" i get compile errors
    29.             playerRb.AddForce(Vector3.up * 10 * jumpForce, ForceMode.Impulse);
    30.             isOnGround = false;
    31.             playerAnim.SetTrigger("Jump_trig");
    32.             {
    33.  
    34.             }
    35.         }
    36.     }
    37.     // here is the first new reference to "other" this is where I am not sure how
    38.     // to fix this since it appears that the reference is invalid how do I fix this?
    39.         private void OnCollisionEnter(Collision collision other)
    40.     {
    41.             if (other.gameObject.CompareTag("Ground"))
    42.         {
    43.             isOnGround = true;
    44.         }   else if (other.gameObject.CompareTag("Obstacle"))
    45.         {
    46.             Debug.Log("Game Over Woody");
    47.             gameOver = true;
    48.             playerAnim.SetBool("Death_b", true);
    49.             playerAnim.SetInteger("DeathType_int", 1);
    50.         }
    51.     }
    52. }
    53.  
    I am attempting to complete the Tutorial for "Create with Code" Lesson 3.3 Don't Just Stand There. Step 6 Set up a Falling Animation I'm having problems with the code where it is referencing "other" but it appears it was never defined so I'm getting compile errors. I'm new to all of this but determined to learn.

    I am also having problems with the reference to the variable jumpFoce it shows in the video example the playerRb.AddForce(Vector3.up * 10 jumpForce, ForceMode.Impulse); but this creates errors as well so I removed the "10" and the errors went away is this correct?

    Here is my code for the PlayerController.cs

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class PlayerController : MonoBehaviour { private Rigidbody playerRb; private Animator playerAnim; public float jumpForce = 10; public float gravityModifier; public bool isOnGround = true; public bool gameOver = false;
     
    Last edited: Jan 11, 2020
  2. Ufreewoody

    Ufreewoody

    Joined:
    Nov 3, 2017
    Posts:
    16
    Here I'm trying to get it right Bill.
     
  3. JacekMackiewicz

    JacekMackiewicz

    Unity Technologies

    Joined:
    Jul 5, 2017
    Posts:
    34

    Hi there Ufreewoody,

    Apologies you ran into this issue! These errors were indeed caused by the "other" contained within the OnCollisionEnter function. This was included in the code screenshot by mistake - simply removing "other" from the code will fix your errors!
    Within the function we're checking for a Collision that we're naming "collision" - "other" is unnecessary as it doesn't do anything in this context, thus confusing the compiler and causing errors.

    I've updated the code screenshot in the tutorial now so it won't be an issue in the future - thanks for reporting this to us!
    Good luck with the rest of the course :)