Quantcast
Channel: Questions in topic: "class instance"
Viewing all articles
Browse latest Browse all 131

Passing an instance of a class to different game objects

$
0
0
I am trying to make a product catalog so we can show products, take orders, etc when on different devices and offline to our store. At the moment I am having trouble passing a instantiated class to another gameobject. I checked some of the similar questions but they did not help me (or I couldn't get them to work with my situation) The way I have it set up is all through UI elements. First I have the Product Class that holds all the information for a product: using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; public class Product : MonoBehaviour { public string productName; public Image productImage; public string productSKU; public float productPrice; public List categories; } Then I have a product master where I create all the products: public class ProductMaster : MonoBehaviour { public static ProductMaster instance; public List product; //List to hold all product data // Use this for initialization void Awake(){instance = this;} void Start () { AddProducts (); } void AddProducts() { Product Item1 = new Product (); Item1.productName = "A Ball"; Item1.productSKU = "BALL"; Item1.productPrice = 39.99f; Item1.productDescription = "Details1,\n followed by details 2"; Item1.productImage = (Image)AssetDatabase.LoadAssetAtPath("Assets/Images/item-1.jpg", typeof(Image)); product.Add (Item1); Product Item2 = new Product (); Item2.productName = "Another Ball"; Item2.productSKU = "BALL2"; Item2.productPrice = 19.99f; Item2.productDescription = "Details1,\n followed by details 2"; Item2.productImage = (Image)AssetDatabase.LoadAssetAtPath("Assets/Images/item-2.jpg", typeof(Image)); product.Add (Item2); } } And so on for all the products. This all works fine and gets the data into the list. The next step is that I need to instantiate all the products of a certain category into thumbnails on a canvas. I do this with the following: public class CategoryProducts : MonoBehaviour { ProductMaster PM; public GameObject productThumbnail; public Canvas viewPort; // Use this for initialization void Start () { PM = ProductMaster.instance; } // Update is called once per frame void Update () { if (Input.GetKeyDown ("c")) FillCategory ("Balls"); } void FillCategory(string category) { foreach (Product p in PM.product) { GameObject go = Instantiate (productThumbnail) as GameObject; //productThumbnail has a Product Script on it go.name = p.productName; go.transform.SetParent (viewPort.transform, false); go.GetComponent() = p; //THIS IS THE PART THAT DOES NOT WORK. HOW CAN I ASSIGN THE DATA FROM p INTO THE PRODUCT ON THE productThumbnail? Debug.Log (p.productSKU + " " + p.productName); } } } The instantiated gameobject from the above code has a product class attached to it that I want to assign the data from the PM.product List but it spits out the error: The left-hand side of an assignment must be a variable, a property or an indexer The instantiated object is also a button that when the user presses it, it passes the product data again into a canvas that displays all the information such as productName, productPrice, etc. How do you pass an instance of a class full of data to another gameobject? Or is there another way I should be tackling this issue? I think the best way is for all the parts of the program (there will be 3 canvasses where the same product info need to go to) to reference the one master product list rather than recreating the product in each instance. Cheers

Viewing all articles
Browse latest Browse all 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>