- Home /
Implementing Setter/Getter
So I've been trying to build an item pick up system in a game, but I've run into a bit of a problem. The system uses two scripts, one to mark the items as held, and another to handle picking up the item itself. The add item to inventory script looks like this
public void foundItem(int symbolNum){
//Iterates through all the clue items based off the length of images there are
//If the itemNum = images[ itemNum - 1] you have found the item
//Set the item that you have found to isFound = true
//Set the newly found item image to a visible transpency
print ("Looking for item# "+symbolNum);
//If the clue value is the same as symbol value, proceed
GameObject[] clueSymbolGOs = GameObject.FindGameObjectsWithTag("ClueSymbol");
//We are now looking for the clueItem i that matches s (symbol number)
foreach(GameObject clue in clueSymbolGOs){
if(clue.GetComponent<ClueBehaviour>().getClueSymbol() == symbolNum){
print ("Found clue symbol game object # "+ symbolNum);
//clue.GetComponent<GUITexture>().texture = symbolHolder;
//this.gameObject.GetComponent<GUITexture> ().texture = images[itemNum];
clue.GetComponent<ClueBehaviour>().isFound = true;
clue.GetComponent<GUITexture>().color = new Color(1f, 1f, 1f, 1f);
}
}
}
Granted, there's more to that script than this, but this is the only part that pertains to what I'm doing. The second script functions by feeding a number to the int symbolNum, allowing the first function to know what to check off as found. The code I have to feed the number looks like this
void OnTriggerStay2D() {
if (Input.GetMouseButtonDown (0)) {
Symbolmatch = (GetComponent<ClueBehaviour>().foundItem(2));
Destroy (this.gameObject);
//Find.gameObject("Clue2").GetComponent<ClueBehaviour>().clueSymbol = 2;
Debug.Log ("clicked");
}
}
Looking around has told me that a getter/setter is the best way to make this work, but I don't know how to set one of those up properly, despite looking at a number of example and tutorial videos. Can someone help me get this working?
what makes you think that getters/setters are right for this? which examples/tutorials did you look at?
there doesn't seem to be enough info presented to understand why you'd use them, or for which parts.
I had a previous question open last night where the person said that was the best option, after solving a bunch of other problems with the script. Why, do you know a better method? All I'm trying to do is have the second script find the first script and give a value to int symbolNum. How would you suggest I go about this? The main issue I have right now is that I can't access foundItem from the second script because it is a void function.
sounds like you need to read up on c# a little. there's not really anything called a void function - what you're referring to is a function that doesn't return anything, hence the void.
again, without more info or reference to your previous question, it appears that you just need to be careful with get component usage and ensure that the variables or functions in the other scripts are marked as public, unless the script referencing it is derived from it. maybe some more c# to read up on...
If I wasn't typing this on my iPad from the couch, I'd be able to rewrite your code to demonstrate what I mean.
That is incorrect, unfortunately. The code cannot run because I'm getting the error 'cannot implicitly convert type void to unityengine.gameobject. I'm unsure what variables you are referring to that aren't labeled public.
All I'm trying to do here is make it so that when the second script is triggered, the int symbolNum will get the value I want. And I've been trying to find information on this problem, but the closest I've been able to find was something in Javascript, which doesn't really help me. Is there a simpler way to do this? Can you show me?