Hi, I’m using a script in witch I use an int variable called Money in the script MoneyRecup.
I try to access it from this script but it didn’t work the value steal at 0.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mechants : MonoBehaviour
{
Rigidbody2D rb;
GameObject target;
float movespeed;
Vector3 directionToTarget;
public GameObject explotion;
public GameObject Player;
private bool Rebond;
public float force = 20f;
public MoneyRecup b;
void Start()
{
target = GameObject.Find ("Player");
rb = GetComponent<Rigidbody2D>();
movespeed = 3f;
b = Player.GetComponent<MoneyRecup>();
Debug.Log(b.Money);
}
void Update()
{
Move();
}
void OnTriggerEnter2D (Collider2D col)
{
if(col.gameObject.tag == "Player")
{
if(b.Money <= 0)
{
Spawn_Mechants.spawnAllowed = false;
Destroy (col.gameObject);
}else{
b.Money -= 1;
Destroy(gameObject);
}
}
if(col.gameObject.tag == "Wall")
{
Destroy(gameObject);
}
}
void Move()
{
if(target != null)
{
directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = new Vector2 (directionToTarget.x * movespeed, directionToTarget.y * movespeed);
}
else
{
rb.velocity = Vector3.zero;
}
}
}