- Home /
This question was
closed Nov 07, 2016 at 06:33 PM by
Nasilowski for the following reason:
Other
Question by
Nasilowski · Nov 07, 2016 at 07:42 AM ·
multiple objects
Use one script for multiple objects
I've made an shop where i want to be able to select a player and make it show information about it.
Script 1: Gathers information from multiple objects (player icons)
using UnityEngine;
using System.Collections;
public class BuyOrSelect : MonoBehaviour {
public int PlayerNumber;
public GameObject[] Buttons;
//Button for the player icons
public void ShowPlayerInfo(){
GameObject thePlayer = GameObject.FindGameObjectWithTag ("PlayerIcon");
PlayerInfoShop ShopInfo = thePlayer.GetComponent<PlayerInfoShop>();
int boughtStatus = ShopInfo.BoughtStatus;
PlayerNumber = ShopInfo.PlayerNumber;
//not selected
if (boughtStatus == 0) {
Buttons[0].gameObject.SetActive(false);
Buttons[1].gameObject.SetActive(false);
}
//Bought
if (boughtStatus == 1) {
Buttons[1].gameObject.SetActive(true);
Buttons[0].gameObject.SetActive(false);
}
//Not bought
if (boughtStatus == 2) {
Buttons[0].gameObject.SetActive(true);
Buttons[1].gameObject.SetActive(false);
}
}
}
Script 2: Is attached to every player icon with different values on each.
using UnityEngine;
using System.Collections;
public class PlayerInfoShop : MonoBehaviour {
//which player
public int PlayerNumber;
//if the object is bought or not
public int BoughtStatus;
//object price
public int price;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
The issue is that it for some reason only reads one of the player icon values, if it makes any sense.
b6f4ade36ae34aa48930a823567a3463.png
(50.9 kB)
Comment
Follow this Question
Related Questions
Count multiple objects! 3 Answers
overlapping of enemy 2 Answers
Multi-gun Aiming System 1 Answer
Only getting one game object instead of multiple game objects in start of the program 0 Answers
How to keep objects disabled after reloading the scene? 2 Answers