- Home /
Question by
Upen787 · Jun 12, 2020 at 11:55 AM ·
scripting problemcollider2dscoreboard
Access the data of OnTriggerEnter2D function of a script from the other Script, how do I it?
.......................Script whose OnTrigeer I want to access.....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreScript : MonoBehaviour
{
private Text CoinTextScore;
private AudioSource AudioManager;
private int Score = 0;
void Awake()
{
AudioManager = GetComponent<AudioSource>();
}
void Start()
{
CoinTextScore = GameObject.Find("CoinText").GetComponent<Text>(); //Picking up UI element
}
void Update()
{
}
void OnTriggerEnter2D(Collider2D target)
{
if(target.tag == "Coin")
{
target.gameObject.SetActive(false);
Score++;
CoinTextScore.text = "x" + Score; //We can either convert Score to String or by String Concatenation add a string and join the int to it
AudioManager.Play();
}
}
}
.................. Script where I want to use.............
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BonusScript : MonoBehaviour
{
public Transform BottomCollision;
private Animator anim;
public LayerMask PlayerLayer;
private Vector3 MoveDirection = Vector3.up;
private Vector3 OriginPosition;
private Vector3 AnimPosition;
private bool StartAnim;
private bool canAnimate;
private Text BonusScore;
private int Bonus =10 ;
private void Awake()
{
anim = GetComponent<Animator>();
canAnimate = true;
BonusScore = GameObject.Find("BonusText").GetComponent<Text>();
}
void Start()
{
OriginPosition = transform.position; //Original Position of the Block
AnimPosition = transform.position;
AnimPosition.y += 0.15f; //Block moves up
}
void Update()
{
CheckForCollision();
AnimateUpDown();
}
void CheckForCollision()
{
if (canAnimate)
{
RaycastHit2D Hit = Physics2D.Raycast(BottomCollision.position, Vector2.down, 0.1f, PlayerLayer);
if (Hit)
{
if (Hit.collider.gameObject.tag == "Player")
{
***I want to use the CoinScore in OnTrigger here, from above Script****
Bonus++;
BonusScore.text = "x" + Bonus;
anim.Play("IdleBlock");
StartAnim = true;
canAnimate = false;
}
}
}
}
void AnimateUpDown()
{
if (StartAnim)
{
transform.Translate(MoveDirection * Time.smoothDeltaTime); //Moves Block Up when Anim Starts
if(transform.position.y >= AnimPosition.y) //We're going up to AnimPosition
{
MoveDirection = Vector3.down; //Going Down
}
else if(transform.position.y <= OriginPosition.y) //When we reach Original POsition
{
StartAnim = false;
}
}
}
}
,
Comment
Answer by BreyTheKid · Jun 12, 2020 at 11:59 AM
You could make a boolean variable that is true when OnTriggerEnter is true and false when it is false. You can then reference the first script in the second script and access that variable. Hope this helps!
Answer by Unity-Devloper · Jun 12, 2020 at 12:59 PM
List item
Make First Instance of "ScoreScript" class
public static ScoreScript Instance ;
initiate ScoreScript Instance into Awake() Function
void Awake() { Instance=this; }
Store Your Collision Tag Into Variable
var tag = int.Parse(collision.gameObject.tag);
if you use Second Script Function Then Use Like This Also BonusScript Instance Needed like 1. List item
BonusScript .Instance.BonusPower(tag);
Hope this helps Better!