- Home /
Can I make Money collecting script without attaching it to a object ? I tried but I got error
When i am trying to make my money variables file without attaching it to object it stuck in 5 how to fix it ? It only works when monevariables.cs is a component
moneysystem.cs attached to object called moneyonground(a basic cube ) and the money variables attaced to variablesobj in order to work
CODE GIVES ME ERROR Moneysystem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moneysystem : MonoBehaviour
{
moneyvariables mv = new moneyvariables();
bool intrigger;
// Use this for initialization
void Start() {
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
intrigger = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
intrigger = false;
}
}
// Update is called once per frame
void FixedUpdate () {
if(intrigger)
{
if (Input.GetKeyDown(KeyCode.G))
{
mv.money = mv. money + mv.moneyonpaper;
Debug.Log(mv.money);
Destroy(gameObject);
}
}
}
}
MoneyVariables.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moneyvariables : MonoBehaviour
{
public int money;
public int moneyonpaper = 2;
public int cost;
}
$$anonymous$$y friend, you are asked the same question several time, you need to understand how works.
You have your class moneyvariables:
public class moneyvariables : $$anonymous$$onoBehaviour
{
public int money;
public int moneyonpaper = 2;
public int cost;
}
Ok, its fine, but imagine that class like a bag of money. You can get your bag, and I can get my bag, both bags are the same Class (moneyvariables) but are different bags.
When you writes this:
moneyvariables mv = new moneyvariables();
you are making a new bag, the object with that script, is creating a bag for hisself, and when a bag is created, her values are the default values, in this case money = 0, moneyonpaper = 2, cost = 0, then, you are changing the money for that bag, her name is mv, but others objects with same script, are making bags with same name, but each object only knows her bag existence. When you destroy your object, you are destroying the object and all related with them, each bag is inside of each object, then, you makes a bag, you change her value but you destroy the bag with the object.
You need a unique bag, all objects have to change the money on the same bag, this is the cause you are making a cube on the game with the component, all objects on your scene need to say the bag of that cube add or delete her money. You can have 2 bags:
public moneyvariables bag1 = new moneyvariables();
public moneyvariables bag2 = new moneyvariables();
//This is making 2 diferent bags, but only on this object, if you destroy the object with this script, both bags destroyed too.
// If the name of this gameObject is "cube", you can change the values of both bags
// Find the object with bags
GameObject.Find("cube").gameObject<SCRIPT NA$$anonymous$$E WITH BOTH NEW BAGS>().bag1.money = 12398123;
GameObject.Find("cube").gameObject<SCRIPT NA$$anonymous$$E WITH BOTH NEW BAGS>().bag2.money = 87891;
It does not matter where you have the script, but you only have to create one and the other objects know where to go
Answer by unidad2pete · Aug 17, 2017 at 05:42 PM
You can´t, you need get the component to change her variables.
if (Input.GetKeyDown(KeyCode.G))
{
GameObject.Find("moneyground").gameObject.GetComponent<moneyvariables>().money + mv.moneyonpaper;
Debug.Log(mv.money);
Destroy(gameObject);
}
i try to explain on a comment on your answer, sorry for my english, im learning ;)
Answer by Igor_Vasiak · Aug 17, 2017 at 05:50 PM
Try this:
public class moneysystem : MonoBehaviour
{
[HideInInspector]public moneyvariables mv = new moneyvariables();
void OnTriggerStay (Collider other)
{
if (other.CompareTag("Player")) {
mv.AddMoneyOnPaper();
}
}
}
public class moneyvariables
{
public int money;
public int moneyonpaper = 2;
public int cost;
public void AddMoneyOnPaper ()
{
money += moneyonpaper; //Writing += is like money = money + moneyonpaper.
}
}
I believe there is no reason to destroy it. Maybe destroy other.gameObject, but not this.gameObject.
Your answer
Follow this Question
Related Questions
Must I attach every script to a gameobject in order to work ? 2 Answers
Script sets same value to other script in all objects instead of just one. 1 Answer
Getting a mix of warnings in multiplayer 0 Answers
Local Multiplayer Game | Input for player 2 completely broken. 0 Answers
How to activate gameObjects based on a boolean from another scene? 1 Answer