Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fedqx · Aug 17, 2017 at 05:34 PM · c#unity 5scripting problemunity5scriptingbasics

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;
 
 }
 

 

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unidad2pete · Aug 17, 2017 at 06:11 PM 1
Share

$$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

avatar image Igor_Vasiak · Aug 17, 2017 at 09:48 PM 0
Share

Something worked?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 
 
 
                 }
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fedqx · Aug 17, 2017 at 06:11 PM 0
Share

but why can you explain to me please ?

avatar image unidad2pete fedqx · Aug 17, 2017 at 06:15 PM 0
Share

i try to explain on a comment on your answer, sorry for my english, im learning ;)

avatar image fedqx unidad2pete · Aug 17, 2017 at 06:23 PM 0
Share

my bad i did not see the comment

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fedqx · Aug 18, 2017 at 07:20 AM 0
Share

it does not worked

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

416 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges