I want to transfer my coin in class 1 to class 2
Class 1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Accelerometer : MonoBehaviour
{
public int coin;
public Text currencyText;
public void OnTriggerEnter(Collider other)
{
if (other.tag == “Coin”)
{
other.gameObject.SetActive(false);
coin += 10;
currencyText.text = coin.ToString();
CoinManager coinManager = other.GetComponent();
coinManager.Totalcoin += coin;
}
}
}
Class 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinManager : MonoBehaviour {
private static CoinManager instance;
public static CoinManager Instance { get { return instance; } }
public Text CoinText;
public int Totalcoin;
public void Update()
{
Totalcoin = 1000;
CoinText.text = Totalcoin.ToString();
}
}
not sure if this is what you mean, but in your class 2 you can just write
public class CoinManager : Accelerometer
then get& set your variables
no change at all, but i have got some error message
“NullReferenceException: Object reference not set to an instance of an object
Accelerometer.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Accelerometer.cs:52)”
btw thanks for your help
hmm still not 100% sure what you are saying what you want to do. are you looking to trash the class 1 and just use the class2?
in your null reference exception nothing is set for the “OnTriggerEnter”, whatever you are trying to collide with needs to be assigned
also it helps to use code tags look here
If you see someone who needs to know about code tags, please link them to this post: Please use code tags when posting code. You can "tag" your code by typing around your code. There is no overt "Code" tag button that automatically tags a...
Reading time: 9 mins 🕑
Likes: 83 ❤
in my idea, i want to make a game ball with an endless way. and collect coin using class 1 (class 1 is script ball) ex : tample run game. if i collect coin, variable coin increases ++ and then transfer coin to totalcoin in class 2. for using in the shop game.
sorry if im not good at english
eehhmmm i forgot it, i have 2 scene, class 2 at scene 1 and class 1 at scene 2