Question by
harrywhitelegg_unity · Mar 05, 2020 at 10:28 PM ·
scripting problem
Pickup script not working, pls help
My pickup script for some reason randomly picks the object up twice and sometimes not. Super confused, I added a debug.log to the calculation expression and randomly the calculation is made twice.
The first calculation was registered properly but the second one registered twice.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour {
public CharacterController2D controller;
public float runSpeed = 40f;
public Animator animator;
float horizontalMove = 0f;
bool jump = false;
public Text countText;
public int count;
void Start()
{
count = 0;
SetCountText ();
}
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
}
}
public void OnLanding()
{
animator.SetBool("IsJumping", false);
}
void FixedUpdate()
{
// Move our character
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
jump = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
Debug.Log(count = count + 10);
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
}
debug.png
(7.5 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612224441im_/https://answers.unity.com/themes/thub/images/avi.jpg)