Hello, I am new to coding and I am having trouble figuring out how to do this.
For my game, I am trying to make each piece of food worth a different point value. Like a burger is 20 points and bread is 15. I thought I should do it by making a public float to assign that point value. When the item enters the goal zone, the score goes up.
Right now the script for the food item is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class pointworth : MonoBehaviour
{
public float foodvalue;
The code for the goal zone is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class goalarea : MonoBehaviour
{
public logicscripts logic;
// Start is called before the first frame update
void Start()
{
logic = GameObject.FindGameObjectWithTag("logic").GetComponent<logicscripts>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
logic.addScore();
}
}
I have a separate code for the score counter which is :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class logicscripts : MonoBehaviour
{
public float playerScore;
public Text scoreText;
public pointworth script;
public GameObject Square;
public global::System.Single PlayerScore { get => playerScore; set => playerScore = value; }
public void addScore()
{
PlayerScore = PlayerScore + 1;
scoreText.text = PlayerScore.ToString();
}
}
I am using unity 2d, does anyone have any suggestions? I’ve tried to create a tag and use find object with tag to no avail. Right now, if I use this script the score only goes up by 1, I am trying to assign specific objects their own score value.