Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thereddic · Nov 11, 2014 at 10:45 PM · getcomponentgetter

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Nov 11, 2014 at 08:28 PM 0
Share

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.

avatar image Thereddic · Nov 11, 2014 at 08:52 PM 0
Share

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.

avatar image gjf · Nov 11, 2014 at 09:26 PM 0
Share

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.

avatar image Thereddic · Nov 11, 2014 at 09:33 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

An issue with GetComponent 2 Answers

Accessing Scripts from a different Script 1 Answer

Running a script function using "GetComponent" 1 Answer

Using multiple instances of the same script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges