Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity Visual Money System

Discussion in 'Scripting' started by Will-Metcher, Nov 15, 2014.

  1. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    Hey guys
    I have been wondering about C#'s adding/removing prefabs system. Basically I am Designing a game in which the player earns money(Doing a variety of different jobs) which can be spent on upgrades etc.. In the players 'Base' I have put in a room that houses all the money(gold bars/coins). I am trying to implement a system where for example each pile of gold is worth 10k and based on the amount of money they have it creates/ destroys the piles(10k = 1 pile 20k = 2 piles and so on). I have tried using Instantiate but could not get it working(kinda new at this) and from what I can guess this is a large part of Unity. I will paste my code below but it is pretty broken. The gold pile prefab is called Gold_Ingots and the money variable is simply called money.



    using UnityEngine;
    using System.Collections;

    public class MoneyDisplay : MonoBehaviour
    public GameObject Gold_Ingots;
    {



    // Use this for initialization
    void Start ()
    {
    int money = 5
    }

    // Update is called once per frame
    void Update ()
    using UnityEngine;
    using System.Collections;

    public class ExampleClass : MonoBehaviour {
    public Transform prefab;
    void Example() {
    int money = 5;
    while (i > 0) {
    Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
    i++;
    }
    }
    }
    }

    thanks for the support I really appreciate it
     
  2. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    please use the codetags so we can understand the code better.
     
    Magiichan likes this.
  3. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    I reformat your code, but unfortunately it doesn't help much.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoneyDisplay : MonoBehaviour
    5. public GameObject Gold_Ingots;
    6. {
    7.  
    8.  
    9.  
    10.    // Use this for initialization
    11.    void Start ()
    12.    {
    13.       int money = 5
    14.    }
    15.  
    16.    // Update is called once per frame
    17.    void Update ()
    18.    {
    19.       using UnityEngine;
    20.       using System.Collections;
    21.  
    22.       public class ExampleClass : MonoBehaviour
    23.       {
    24.          public Transform prefab;
    25.          void Example()
    26.          {
    27.             int money = 5;
    28.             while (i > 0)
    29.             {
    30.                Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
    31.                i++;
    32.             }
    33.          }
    34.       }
    35.    }
    36. }
    I'm not able to get the idea of this code, what exact did you try to achieve?
     
  4. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    I wanted to get it to duplicate the prefab and place it in the game(pretty sure right on top of the original object)
     
  5. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    I just relooked at the code and was waondering are the parameters in line 30 the problem?
     
  6. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    Just did it again in JS.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var goldPile : Rigidbody;
    4. public var goldPlaceHolder : Transform;
    5. var money = 5;
    6. var space1 = 1;
    7.  
    8. function Update ()
    9. {
    10.     if(money === 2)
    11.     {
    12.         if(space1 === 1)
    13.         {
    14.             var goldInstance : Rigidbody;
    15.             goldInstance = Instantiate(goldPile, goldPlaceHolder.position, goldPlaceHolder.rotation);
    16.                var space1 = 0;
    17.         }
    18.     }
    19. };
    I get two errors both saying "'is' can't be used with a value type ('int')"
    I assume that there is a problem with the if statements. I have tried using && instead of the two statements but was still having the same problem. Any Ideas would be greatly appreciated.
     
  7. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    Well, === is a comparator that is barely used, and don't compare values but object instances.
    int not being a type that supports it, it gives you errors.

    What you want is a value comparison operator == .

    Also, might be unrelated, but your "goldInstance" variable can't be used outside of the IF, just pointing it out, because it might not be on purpose (it has its uses too though)
     
    pezz likes this.
  8. Will-Metcher

    Will-Metcher

    Joined:
    Nov 15, 2014
    Posts:
    14
    Th