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++;
}
}
}
}
I reformat your code, but unfortunately it doesn’t help much.
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++;
}
}
}
}
}
I’m not able to get the idea of this code, what exact did you try to achieve?
#pragma strict
public var goldPile : Rigidbody;
public var goldPlaceHolder : Transform;
var money = 5;
var space1 = 1;
function Update ()
{
if(money === 2)
{
if(space1 === 1)
{
var goldInstance : Rigidbody;
goldInstance = Instantiate(goldPile, goldPlaceHolder.position, goldPlaceHolder.rotation);
var space1 = 0;
}
}
};
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.
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)