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 06:42 AM · getcomponentbool

Can't Call function in another script

I'll keep this brief. I'm building an inventory system with a friend. The code that registers the item in the inventory of the player is thus

 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);
             }
         }
     
     }

Obviously that's not the entire code, but that's the segment I'm working with. I need to make it so that when I find an item, it runs this function in order to add the item to the inventory. The current code I have for that is thus

 void OnTriggerStay2D() {
     if (Input.GetMouseButtonDown (0)) {
         Destroy (this.gameObject);
         GetComponent<ClueBehaviour>().foundItem(2);
         //GameObject.Find ("Clue2").GetComponent<ClueBehaviour> ().isFound = true;
         Debug.Log ("clicked");
     }
 }

The way this works is that when the player is in contact with the item, clicking on it will destroy the item on the map and change the item in the inventory to be registered as found. However, I am getting an error stating "Object Reference is not set to an instance of an object" when the item is picked up. The item will delete as is proper, but the isFound Bool is unchanged. What am I doing wrong? THanks in advance.

Comment
Add comment · Show 1
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 MD_Reptile · Nov 11, 2014 at 07:06 AM 1
Share

If you are using get component on this game object, just after destroying it, it's going to be null. You must have that script attached to another game object, and do getcomponent through the other object, like "otherGameObject.GetComponent..."

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by bubzy · Nov 11, 2014 at 07:04 AM

  void OnTriggerStay2D() {
      if (Input.GetMouseButtonDown (0)) {
          Destroy (this.gameObject); //here! you are destroying the gameObject and then on the line below
          GetComponent<ClueBehaviour>().foundItem(2); //you are trying to do something with the destroyed gameObject 
          //GameObject.Find ("Clue2").GetComponent<ClueBehaviour> ().isFound = true;
          Debug.Log ("clicked");
      }
  }
Comment
Add comment · Show 17 · Share
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 MD_Reptile · Nov 11, 2014 at 07:08 AM 0
Share

This is what I meant in my comment above, thumbs up!

avatar image bubzy · Nov 11, 2014 at 07:09 AM 0
Share

its from experience, done it a couple of times :) ty for thumbs

avatar image Thereddic · Nov 11, 2014 at 07:11 AM 0
Share

Alright, fixing that sequencing got it to find the foundItem function properly, and I had to set a game object in the second script so that something could GetComponent. Now I'm running into the problem of being unable to call void functions. Is there a workaround here?

avatar image MD_Reptile · Nov 11, 2014 at 07:13 AM 0
Share

$$anonymous$$ake sure they are public void $$anonymous$$y$$anonymous$$ethod()

avatar image MD_Reptile · Nov 11, 2014 at 07:16 AM 0
Share

Also be sure to mark the answer as correct! (Little check mark in a circle)

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Simple question about OnMouseButtonAsUp 1 Answer

Any problems with this script? 1 Answer

check if the bool is true from other script c# 1 Answer

Error accessing var from other scripts within project via GetComponent 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