Multiple GameObjects same script problem (driving me nuts please help)
Hey, Im making a game where you can acess a store. The store works perfectly until I add another store in the scene. Whenever I click buy on the second created store, the gameobject instantiates in the first store's spawn point. This doesnt make sense to me since the second store has its own spawn point, far away from the first store's spawn point, however using the same script. Take a look at my script:
public class Store : MonoBehaviour
{
public GameObject killCount;
KillCount theCount;
public int[] prices;
void Start()
{
theCount = killCount.GetComponent<KillCount>();
StoreUI.gameObject.SetActive(false);
//Setting up prices
prices[0] = 250;
prices[1] = 300;
prices[2] = 400;
prices[3] = 500;
prices[4] = 600;
prices[5] = 600;
prices[6] = 750;
prices[7] = 750;
prices[8] = 750;
prices[9] = 750;
prices[10] = 750;
prices[11] = 750;
prices[12] = 850;
prices[13] = 1000;
}
public void GivePistol()
{
if (theCount.currentKillCount >= prices[0])
{
Instantiate(weapons[0].transform, spawnPoint.transform.position, Quaternion.identity);
UnsufficientFunds.gameObject.SetActive(false);
}
else
{
UnsufficientFunds.gameObject.SetActive(true);
}
}
public void GivePistolDW()
{
if (theCount.currentKillCount >= prices[1])
{
Instantiate(weapons[1].transform, spawnPoint.transform.position, Quaternion.identity);
UnsufficientFunds.gameObject.SetActive(false);
}
else
{
UnsufficientFunds.gameObject.SetActive(true);
}
}
}
On the Script KillCount I wrote this:
public class KillCount : MonoBehaviour
{public int currentKillCount = 10000;
public GameObject Store;
Store theStore;
public void BuyPisltol()
{
currentKillCount -= theStore.prices[0];
text.text = TEXT_PREPEND + currentKillCount.ToString();
}
public void GivePistolDW()
{
if (currentKillCount > theStore.prices[1])
{
currentKillCount -= theStore.prices[1];
text.text = TEXT_PREPEND + currentKillCount.ToString();
}
else
{
Debug.Log("Unsufficient funds");
}
}
}
Comment
Answer by Tariq-mehmood · Aug 25, 2017 at 01:55 PM
IDK but this spawnPoint variable is not initialized anywhere but what I am understanding is that the same spawnPoint variable is used in both function GivePistolDW() and GivePistol() that's why this is happening
Your answer
![](https://koobas.hobune.stream/wayback/20220612133509im_/https://answers.unity.com/themes/thub/images/avi.jpg)