I'm making 2D Game like Pac-Man. Nevertheless, I have some issues to create a scoring system.
I want to update score whenever my Pacman eat coin(a.k.a pacdot)
I made C# script called 'ScoreManager'
Here is code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScoreManager : MonoBehaviour {
static int score = 0;
public static void setScore(int value)
{
score += value;
}
public static int getScore()
{
return score;
}
void OnGUI ()
{
GUILayout.Label("Score: " + score.ToString());
}
}
This code is working well when I play my game in Unity engine
But, I don't know how to set up ScoreValue in Pacdot scripts.
Here is Pacdot Code
using UnityEngine;
using System.Collections;
public class Pacdot : MonoBehaviour {
public int score = 10;
void OnTriggerEnter2D(Collider2D co) {
if (co.name == "pacman")
{
Destroy(gameObject);
}
}
}
Also, I added C# Script ( Pacmanbehaviour )
using UnityEngine;
using System.Collections;
public class PacmanMove : MonoBehaviour {
public float speed = 0.4f;
Vector2 dest = Vector2.zero;
void Start() {
dest = transform.position;
}
void FixedUpdate() {
// Move closer to Destination
Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
// Check for Input if not moving
if ((Vector2)transform.position == dest) {
if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
dest = (Vector2)transform.position + Vector2.up;
if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
dest = (Vector2)transform.position + Vector2.right;
if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
dest = (Vector2)transform.position - Vector2.up;
if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
dest = (Vector2)transform.position - Vector2.right;
}
// Animation Parameters
Vector2 dir = dest - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
}
bool valid(Vector2 dir) {
// Cast Line from 'next to Pac-Man' to 'Pac-Man'
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
}
ScoreManager script needs to live as a game object, or as a component in a game object. Then you can add it as a field in your Pacdot class.
It'll be something like this below, but finding the specific game object the script is attached to will depend on how you have it designed (the "find by tag" approach won't work unless you have a game object with ScoreManager attached, with that tag).
using UnityEngine;
using System.Collections;
public class Pacdot : MonoBehaviour {
public int score = 10;
private ScoreManager _score = GameObject.findGameObjectWithTag("scoreKeeper").GetComponent<ScoreManager>();
void OnTriggerEnter2D(Collider2D co) {
if (co.name == "pacman")
{
_score.SetScore(score);
Destroy(gameObject);
}
}
}
I would also look at the answer @derHugo linked to--a lot of ways to accomplish this, depending on your needs/design.