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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I am attempting to create coins in my 2D game

Discussion in 'Scripting' started by pryog1, Apr 29, 2019.

  1. pryog1

    pryog1

    Joined:
    Mar 14, 2017
    Posts:
    5
    I am using the prefab by unity technologies from the asset store for my 2d character (it is called "character robot boy" from the "standard assets" package). I am new to coding (scratch that, COMPLETE noob) but decided to attempt to make some coins in my 2d game. After some Google searches I found a tutorial, and added some code (the code at the very bottom) to my character script. Unfortunately, it has given me multiple errors. I noticed that when coding, visual studio said "Destroy" does not exist in the current context. Then when I try to run my game it says "all compiler errors have to be fixed before entering play mode." When I look at the inspector for my character (robot boy prefab), the two scripts with it say they cannot be loaded, and that I need to fix compiler errors and assign valid scripts.


    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace UnityStandardAssets._2D
    5. {
    6.     public class PlatformerCharacter2D : MonoBehaviour
    7.     {
    8.         [SerializeField] private float m_MaxSpeed = 10f;                    // The fastest the player can travel in the x axis.
    9.         [SerializeField] private float m_JumpForce = 400f;                  // Amount of force added when the player jumps.
    10.         [Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;  // Amount of maxSpeed applied to crouching movement. 1 = 100%
    11.         [SerializeField] private bool m_AirControl = false;                 // Whether or not a player can steer while jumping;
    12.         [SerializeField] private LayerMask m_WhatIsGround;                  // A mask determining what is ground to the character
    13.  
    14.         private Transform m_GroundCheck;    // A position marking where to check if the player is grounded.
    15.         const float k_GroundedRadius = .2f; // Radius of the overlap circle to determine if grounded
    16.         private bool m_Grounded;            // Whether or not the player is grounded.
    17.         private Transform m_CeilingCheck;   // A position marking where to check for ceilings
    18.         const float k_CeilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
    19.         private Animator m_Anim;            // Reference to the player's animator component.
    20.         private Rigidbody2D m_Rigidbody2D;
    21.         private bool m_FacingRight = true;  // For determining which way the player is currently facing.
    22.  
    23.         private void Awake()
    24.         {
    25.             // Setting up references.
    26.             m_GroundCheck = transform.Find("GroundCheck");
    27.             m_CeilingCheck = transform.Find("CeilingCheck");
    28.             m_Anim = GetComponent<Animator>();
    29.             m_Rigidbody2D = GetComponent<Rigidbody2D>();
    30.         }
    31.  
    32.  
    33.         private void FixedUpdate()
    34.         {
    35.             m_Grounded = false;
    36.  
    37.             // The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
    38.             // This can be done using layers instead but Sample Assets will not overwrite your project settings.
    39.             Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
    40.             for (int i = 0; i < colliders.Length; i++)
    41.             {
    42.                 if (colliders[i].gameObject != gameObject)
    43.                     m_Grounded = true;
    44.             }
    45.             m_Anim.SetBool("Ground", m_Grounded);
    46.  
    47.             // Set the vertical animation
    48.             m_Anim.SetFloat("vSpeed", m_Rigidbody2D.velocity.y);
    49.         }
    50.  
    51.  
    52.         public void Move(float move, bool crouch, bool jump)
    53.         {
    54.             // If crouching, check to see if the character can stand up
    55.             if (!crouch && m_Anim.GetBool("Crouch"))
    56.             {
    57.                 // If the character has a ceiling preventing them from standing up, keep them crouching
    58.                 if (Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround))
    59.                 {
    60.                     crouch = true;
    61.                 }
    62.             }
    63.  
    64.             // Set whether or not the character is crouching in the animator
    65.             m_Anim.SetBool("Crouch", crouch);
    66.  
    67.             //only control the player if grounded or airControl is turned on
    68.             if (m_Grounded || m_AirControl)
    69.             {
    70.                 // Reduce the speed if crouching by the crouchSpeed multiplier
    71.                 move = (crouch ? move*m_CrouchSpeed : move);
    72.  
    73.                 // The Speed animator parameter is set to the absolute value of the horizontal input.
    74.                 m_Anim.SetFloat("Speed", Mathf.Abs(move));
    75.  
    76.                 // Move the character
    77.                 m_Rigidbody2D.velocity = new Vector2(move*m_MaxSpeed, m_Rigidbody2D.velocity.y);
    78.  
    79.                 // If the input is moving the player right and the player is facing left...
    80.                 if (move > 0 && !m_FacingRight)
    81.                 {
    82.                     // ... flip the player.
    83.                     Flip();
    84.                 }
    85.                     // Otherwise if the input is moving the player left and the player is facing right...
    86.                 else if (move < 0 && m_FacingRight)
    87.                 {
    88.                     // ... flip the player.
    89.                     Flip();
    90.                 }
    91.             }
    92.             // If the player should jump...
    93.             if (m_Grounded && jump && m_Anim.GetBool("Ground"))
    94.             {
    95.                 // Add a vertical force to the player.
    96.                 m_Grounded = false;
    97.                 m_Anim.SetBool("Ground", false);
    98.                 m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
    99.             }
    100.         }
    101.  
    102.  
    103.         private void Flip()
    104.         {
    105.             // Switch the way the player is labelled as facing.
    106.             m_FacingRight = !m_FacingRight;
    107.  
    108.             // Multiply the player's x local scale by -1.
    109.             Vector3 theScale = transform.localScale;
    110.             theScale.x *= -1;
    111.             transform.localScale = theScale;
    112.         }
    113.     }
    114.  
    115.  
    116. private void onTriggerEnter2D(Collider2D other)
    117.     {
    118.         if(other.gameObject.CompareTag("coins"))
    119.         {
    120.             Destroy(other.gameObject);
    121.         }
    122.     }
    123.  
    124.  
    125.  
    126.  
    127.  
    128. }
    129.  
     
  2. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Looks like you've implemented your onTriggerEnter2D method outside the class. Delete the closed bracket } on line 113 and it should work.
     
  3. pryog1

    pryog1

    Joined:
    Mar 14, 2017
    Posts:
    5
    Thanks I'm honestly a complete idiot when it comes to coding. Unfortunately, it still won't run. It says I have a compiler error. Don't know what that is.
     
    Last edited: May 1, 2019