Referencing a public value from one script to another?

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.

Hi,
In OnTriggerEnter you can already get the collider that triggered the method. From there you can access anything in its hierarchy.
So if the pointworth script is on the same object as collider you could access it like this.

     private void OnTriggerEnter2D(Collider2D collision)
    {
        pointworth pw = collision.transform.GetComponent<pointworth>();
    }

That “collision” is the collider that triggered the method.

In your case you can get the values from pointworth scripts and send it to addScore() method.

     private void OnTriggerEnter2D(Collider2D collision)
    {
        float foodVal = collision.transform.GetComponent<pointworth>().foodvalue;
        logic.addScore(foodVal);
    }

And in logicsripts you can add foodValue instead of 1.

    public void addScore(float amount)
    {
        PlayerScore = PlayerScore + amount;
        scoreText.text = PlayerScore.ToString();
    }