fbpx

 

Coding Error?
 

Coding Error?  

   RSS

0

Hey so I've been following along the Course 1B 2.5D Platformer and I have ran into an issue with my coding some where. Well at least I think its my coding. Anyways the issue is that when my character goes through the coins it doesn't collect the coin nor does it remove the coin from the scene. I've looked over my coding a few times and rewatched all the videos and cannot seem to find the issue. Any suggestions?

 

Spoiler
Player.cs

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

public class Player : MonoBehaviour
{
private CharacterController _controller;
[SerializeField]
private float _speed = 5.0f;
[SerializeField]
private float _gravity = 1.0f;
[SerializeField]
private float _jumpHeight = 15.0f;
private float _yVelocity;
private bool _canDoubleJump = false;
[SerializeField]
private int _coins;
private UIManager _uiManager;

// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();

if (_uiManager == null)
{
Debug.LogError("The UI Manager is NULL.");
}
}

// Update is called once per frame
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
Vector3 direction = new Vector3(horizontalInput, 0, 0);
Vector3 velocity = direction * _speed;

if (_controller.isGrounded == true)
{
if (Input.GetKeyDown(KeyCode.Space))
{
_yVelocity = _jumpHeight;
_canDoubleJump = true;
}
}
else
{
if(Input.GetKeyDown(KeyCode.Space))
{
if (_canDoubleJump == true)
{
_yVelocity += _jumpHeight;
_canDoubleJump = false;
}

}

_yVelocity -= _gravity;
}

velocity.y = _yVelocity;

_controller.Move(velocity * Time.deltaTime);
}

public void AddCoins()
{
_coins++;

_uiManager.UpdateCoinDisplay(_coins);
}
}

Spoiler
Coin.cs

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

public class Coin : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Player player = other.GetComponent<Player>();

if (player != null)
{
player.AddCoins();
}

Destroy(this.gameObject);
}
}
}

Spoiler
UIManager.cs

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

public class UIManager : MonoBehaviour
{
[SerializeField]
private Text _coinText;

public void UpdateCoinDisplay(int coins)
{
_coinText.text = "Coins: " + coins.ToString();
}
}

1 Answer
0

Hello SorenKurosaki 

  quick question did you add the parts in the inspector for the canvas the script looks to be right 

and is the on trigger checked on the coin

Share:

 

 

Scroll to Top

Please Login or Register