- Home /
How to access value within instantiated class in different object?
I have a Class called DiamondStats. Within that class is an Int called 'Cut'. I have instantiated this class within the script of an Empty game object that is the parent of Diamond model. During that instantiation 'Cut' is assigned a random int between 1 and 5.
I also have a menu item called CutText which I want to display the Cut int from the instance created. My question is how do I get that value from the instantiated DiamondStats class into the CutText text box?
DiamondStats class
[System.Serializable]
public class DiamondStats
{
// Diamond value
//Carat
//Cut
//Clarity
//Colour
private int Clarity;
private double Carat;
private int Cut;
private int Colour;
private double DiamondValue;
private int[,] point7Topoint89 = new int[,] { { 88, 70, 64, 58, 54, 50, 45, 35, 28, 18, 11 }, { 70, 64, 58, 54, 51, 48, 43, 33, 27, 17, 11 }, { 64, 60, 54, 51, 48, 45, 40, 32, 26, 16, 10 }, { 58, 54, 51, 48, 45, 41, 36, 31, 25, 16, 9 }, { 53, 48, 45, 43, 41, 37, 34, 29, 23, 15, 9 }, { 45, 42, 40, 34, 36, 33, 29, 26, 22, 14, 9 }, { 36, 34, 33, 31, 30, 28, 26, 23, 20, 13, 8 }, { 31, 30, 29, 27, 26, 24, 22, 19, 16, 12, 8 }, { 25, 24, 26, 22, 22, 21, 19, 16, 12, 10, 7 }, { 23, 22, 21, 20, 20, 19, 18, 15, 11, 9, 6 } };
public DiamondStats( int initialClarity, float initialCarat, int initialCut, int initialColour)
{
Clarity = initialClarity;
Carat = initialCarat;
Cut = initialCut;
Colour = initialColour;
DiamondValue = GenerateDiamondValue(Clarity, Carat, Cut, Colour);
Debug.Log("DiamondValue:" + DiamondValue);
}
//class gets
public int getClarity()
{
return Clarity;
}
public double getCarat()
{
return Carat;
}
public int getCut()
{
return Cut;
}
public int getColour()
{
return Colour;
}
public double getDiamondValue()
{
return DiamondValue;
}
//class sets
public void setClarity(int newClarity)
{
Clarity = newClarity;
}
public void setCarat(double newCarat)
{
Carat = newCarat;
}
public void setCut(int newCut)
{
Cut = newCut;
}
public void setColour(int newColour)
{
Colour = newColour;
}
public void setDiamondValue(double newDiamondValue)
{
DiamondValue = newDiamondValue;
}
public double GenerateDiamondValue(int ClarityInput, double CaratInput, int CutInput, int ColourInput)
{
double newDiamondValue = 0;
//int [,] point7Topoint89 = new ;
//int [][] point9Topoint99;
if (CaratInput <= 0.89)
{
newDiamondValue = point7Topoint89[ClarityInput, ColourInput] * 100; // * CutValue variance * variance
}
else if (CaratInput >= 0.90 && CaratInput <= 0.99)
{
}
return newDiamondValue;
}
}
Item script where DiamondStats is initialised:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
public DiamondStats diamond1;
// Start is called before the first frame update
void Start()
{
diamond1 = new DiamondStats(Random.Range(0, 10), Random.Range(0.7f, 0.89f), Random.Range(1, 5), Random.Range(0, 9));
transform.localScale = new Vector3((float)diamond1.getCarat(), (float)diamond1.getCarat(), (float)diamond1.getCarat());
}
// Update is called once per frame
void Update()
{
}
}
TheAppraisalMenu where game menus are changed and the 'Cut' value needs to go
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class AppraisalMenu : MonoBehaviour
{
public GameObject EvaluateSelectionMenu;
public GameObject CutMenu;
public Text CutText;
public GameObject ColourMenu;
public GameObject ClarityMenu;
public GameObject CaratMenu;
public DiamondStats numbers;
public GameObject Diamond;
public void GoToAuction()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void GoToEvaluateSelectionMenu()
{
//load cut UI
EvaluateSelectionMenu.SetActive(true);
CutMenu.SetActive(false);
ColourMenu.SetActive(false);
ClarityMenu.SetActive(false);
CaratMenu.SetActive(false);
}
public void GoToCut()
{
//load cut UI
EvaluateSelectionMenu.SetActive(false);
CutMenu.SetActive(true);
ColourMenu.SetActive(false);
ClarityMenu.SetActive(false);
CaratMenu.SetActive(false);
CutText.text = //< what do I put here?
//Debug.Log("DiamondValue:" + Diamond.GetComponent<Item>.
//Debug.Log("DiamondValue:" + Diamond.GetComponent<DiamondStats>().getCut());
//gameObject.GetComponent<DiamondStats>.
CutText.text = DiamondStats.getCut().ToString(); // returns cut but need to convert cut storage to text values
}
public void GoToColour()
{
//load Colour UI
EvaluateSelectionMenu.SetActive(false);
CutMenu.SetActive(false);
ColourMenu.SetActive(true);
ClarityMenu.SetActive(false);
CaratMenu.SetActive(false);
}
public void GoToClarity()
{
//load Clarity UI
EvaluateSelectionMenu.SetActive(false);
CutMenu.SetActive(false);
ColourMenu.SetActive(false);
ClarityMenu.SetActive(true);
CaratMenu.SetActive(false);
}
public void GoToCarat()
{
//load Carat UI
EvaluateSelectionMenu.SetActive(false);
CutMenu.SetActive(false);
ColourMenu.SetActive(false);
ClarityMenu.SetActive(false);
CaratMenu.SetActive(true);
}
}
Image of editor to show how Hierarchy is laid out. AppraisalMenu is found under: Canvas/ Menu head. Item is attached to DiamondHolder. Please feel free to ask if more info needed.
Answer by $$anonymous$$ · Nov 22, 2021 at 01:34 PM
I didn't test it, but it should work like this:
Diamond.GetComponent<Item>().diamond1.getCut();
Since you typed Diamond as a GameObject in AppraisalMenu Class, you first have to get its Item component. Then since you instantiated your DiamondStats class as diamond1 in your Item's Start() function, your reference to your DIamondStats is stored in the diamond1 property.
Another note from me would be that you could convert all your Getter and Setter methods into regular C# properties like:
public int Clarity { get; set; }
And you would still achieve the same just with a lot less code. Converting them to property would allow you to call - in this case - diamond1.Clarity(value)
for setting it and diamond1.Clarity()
for reading it. If you still needed to do some extra code in the getters, setters, you could still do by tweaking the code a little.
Here's a good tutorial on them https://codeasy.net/lesson/properties
Your answer
Follow this Question
Related Questions
Loading a Constant Array Of Integers 0 Answers
Distribute terrain in zones 3 Answers
Spawn Random Enemy 3 Answers