0
\$\begingroup\$

I need to Collect and Destroy the money but when I add Destroy or an equivalent function, it resets and always says 5 instead of 5-10-15...

I watched videos on YouTube and the only difference we're having is that they do something like this:

Class object = new Class();

I don't think this can cause the problem because when I remove the destroy() function, the problem is solved! But I can't not have this call.

How can I fix this?

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

public class moneysystem : MonoBehaviour {

    bool intrigger;
    variables asd = new variables();
    private void OnTriggerEnter(Collider other)
    {     
        intrigger = true;
    }
    private void OnTriggerExit(Collider other)
    {
        intrigger = false;
    }

    public void FixedUpdate()
    {    
        if (intrigger)
        {
            if (Input.GetKeyDown(KeyCode.G))
            {
                asd.summoney =  asd.money += asd.moneyonpaper;

                Debug.Log(asd.summoney);
                asd.summoney = asd.lastmoney;
                Debug.Log(asd.lastmoney);
                Destroy(this.gameObject); // when I remove This then there is no problem remains
                // I also tried Destroy(gaemobject) and gameobject.setactive(false) and same problem 
                Debug.Log(asd.lastmoney);
            }
        }
    }
}

Variables.cs

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

public class variables : MonoBehaviour {

    public int moneyonpaper = 5;
    public int money;
    public int summoney;
    public int lastmoney;   
}
\$\endgroup\$
2
\$\begingroup\$

It seems like you attached this moneysystem to each gameObject the player can pick up. That means each of these gameObjects has a completely independent moneysystem with its own variables. When you destroy the object, the moneysystem attached to it gets destroyed too, and so do its variables.

There are two solutions for this conundrum:

Clean solution: Separate the money system from the behavior of collectable game objects. Attach the MoneySystem to one invisible controller game object in your scene and the Collectable behavior to each game object which can be collected. The trigger-method of the Collectible would destroy the game object it is attached to and notify the MoneySystem component on the controller game object to change the money count.

Quick&Dirty solution: Just make variables a static variable. Then it is shared by all instances of moneysystem. I am calling this solution "quick" because it would likely fix your problem with just adding one keyword, but "dirty" because this architecture makes it awkward to add any other features than money pickups which interact with the account balance.

\$\endgroup\$
2
\$\begingroup\$

When you collect (press G) the money you destroy the moneysystem by calling Destroy and because your variables are part of your moneysystem, they get destroyed as well. So to prevent resetting of money/summoney you should change how the collection of money works.

\$\endgroup\$
  • \$\begingroup\$ This contains enough information about solving the problem to be a reasonable answer, I'd say. Just make sure you don't use answers as a way to get around the comment restriction — that will just lead to your post being deleted. If the system says you need more rep, then the solution is to get more rep by sharing high-quality questions and answers. ;) \$\endgroup\$ – DMGregory Aug 14 '17 at 12:20
  • \$\begingroup\$ I agree with the restriction and that's why I tried to give an answer why his problem appears and what would be my approach to solve it but with the information he gave I can't specify a solution and normally I would post these requests as a comment and not as an addition to my answer ;) \$\endgroup\$ – Rafiwui Aug 14 '17 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.