How can I pass on the variable from another script to another?

Hello, just in case, yes im new to unity and blablabla,anyways
what I need is a way for this script to know if my player has the isGP variable active and then destroy whatever thing it needs (if y’all need the movement script I can give it)

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

public class TESTBreak : MonoBehaviour
{
[SerializeField] private Transform BreakCheck;

public void Start()
{
gameObject.Find(“player”).GetComponent().NameOfTheProperty = isGP;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == “Player”)
{

}
while (IsGP == true)
{
Destroy(GetComponent());
Destroy(GetComponent());
}
}

}

If you are new to unity and not to programming at all I would suggest a ServiceLocator for this and use services to pass data between classes.

I believe this is not what I need, but I appreciate the gesture!

Referencing variables, fields, methods (anything non-static) in other script instances:

REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

Be sure you understand what you mean by “pass on the variable” because it makes a HUGE difference:

Value Types vs Reference Types:

Also - see the Code<> button on the menu where you are typing, put your code in there please. Cheers.

1 Like