C
C#9mo ago
Fulfinsen

❔ Score increment

Hello. I followed a tutorial on youtube on how to make a small 2D mobile game using Unity. There are some boxes falling from the sky and you (the player) need to dodge them by taping either left or right side of the screen. Everything works fine, but everytime the boxes reaches the middle of the screen, the score increments. How can I do so that the score will increase after the player dodges the box? I will write the code in the thread
7 Replies
Fulfinsen
Fulfinsen9mo ago
Player.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D rb;

// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (touchPos.x < 0)
{
rb.AddForce(Vector2.left * moveSpeed);
}
else
{
rb.AddForce(Vector2.right * moveSpeed);
}
}
else
{
rb.velocity = Vector2.zero;

}
}

void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "block")
{
SceneManager.LoadScene("Game");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
public float moveSpeed;
private Rigidbody2D rb;

// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (touchPos.x < 0)
{
rb.AddForce(Vector2.left * moveSpeed);
}
else
{
rb.AddForce(Vector2.right * moveSpeed);
}
}
else
{
rb.velocity = Vector2.zero;

}
}

void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "block")
{
SceneManager.LoadScene("Game");
}
}
}
Block.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Block : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (transform.position.y < -6f)
{
Destroy(gameObject);
}


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

public class Block : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (transform.position.y < -6f)
{
Destroy(gameObject);
}


}
}
Thinker
Thinker9mo ago
$unity
Fulfinsen
Fulfinsen9mo ago
ah, ok
Thinker
Thinker9mo ago
(generally better for Unity-specifc questions)
Fulfinsen
Fulfinsen9mo ago
I see thanks for the info
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.