• 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 /
  • Help Room /
avatar image
Question by wattse13 · Jul 26, 2021 at 10:54 AM · beginnerfunctionsonclickdelegatesreturn value

How do I use a function’s return value as a parameter in another function?

I am using Unity 2019.4.20f1

Hi, I am currently trying to dynamically set the Game Object value of a button’s OnClick() method. To do this, I am trying to use a delegate event system, which passes along a reference to the clicked-on-GameObject and a couple of functions.

alt text

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 // Attatched to InspectMenuController GameObject
 public class InspectMenuController : MonoBehaviour
 {
     private Transform equipTransform;
     private GameObject currentPrefab;
 
     public GameObject useButton;
     public GameObject replaceButton;
 
     private void OnEnable()
     {
         GameEvents.OnMessageSent += SetCurrentPrefab;
     }
     private void OnDisable()
     {
         GameEvents.OnMessageSent -= SetCurrentPrefab;
     }
    
     // This method is called after a GameObject has been clicked on
     // I think it should take the clicked on GameObject reference as a parameter
     // I don't think it is set up correctly
     public void SetCurrentPrefab(GameObject myClickedPrefab)
     {
         GetCurrentPrefab(myClickedPrefab);
     }
 
     // When this function is called by SetCurrentPrefab it should retrun the passed in GameObject parameter as a value
     // I don't think it is set up correctly
     public GameObject GetCurrentPrefab(GameObject myClickedPrefab)
     {
         myClickedPrefab = currentPrefab;
         Debug.Log("Made it to GetCurrentPrefab");
         return currentPrefab;
     }
 
     // This function should be called with a button OnClick method
     // Ideally, it would use the GetCurrentPrefab return value to dynamically set the GameObject value in the OnClick method
     // I don't think it is set up correctly
     public void CenterPrefab(GameObject currentPrefab)
     {
         currentPrefab = GetCurrentPrefab(currentPrefab);
         if (currentPrefab.TryGetComponent(out EquipmentClass equipment))
         {
             // currentPrefab = myClickedPrefab;
             Debug.Log(equipment.Name);
             Debug.Log("Hi CurrentPrefab");
         }
     }
 }
 

My project is set up in a way that everytime a GameObject is clicked, it triggers a delegate event which passes on a reference to the clicked-on-gameObject to all of the event subscribers. When the delegate event is triggered it calls the function SetCurrentPrefab(GameObject myClickedPrefab). I am using this function mainly to call the next function GetCurrentPrefab(GameObject, myClickedPrefab). Both methods’ arguments should be filled by the currently selected GameObject. However, as I type this I am realizing that I probably don’t have this set up correctly. GetCurrentPrefab() would then return a GameObject as a value, and that GameObject should be the GameObject which was passed along with the delegate event.

Finally, CenterPrefab(GameObject currentPrefab) should pass in GetCurrentPrefab()’s return value as a parameter for its currentPrefab argument. I am using the out parameter to access the selected-GameObject’s components.

As it is currently set up, I am given the error: ArgumentException: Object of type 'UnityEngine.Object' cannot be converted to type 'UnityEngine.GameObject'

When I manually set the OnClick() parameter to the GameObject I am currently testing on I get the error: NullReferenceException: Object reference not set to an instance of an object. This error is triggered by line 45, and I think it has something to do with how I’m using the out parameter and the fact that I am probably not passing my GameObject correctly between the previous three functions.

I’m not sure if this is useful but, in the dropdown menu, I only have one option, CenterPrefab( GameObject), rather than the usual two, static and dynamic.

I’m not a competent coder, and there is a good chance a lot of this is nonsense as I’m not always sure if I am correctly using terms and what-not. I would be very thankful for any help!

screenshot-303.png (25.5 kB)
Comment

People who like this

0 Show 2
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 rage_co · Jul 26, 2021 at 03:15 PM 1
Share

I don't see why you are using myClickedPrefab as a parameter and then setting it to another unrelated value and then returning the other value. i think you want to do

 currentPrefab = myClickedPrefab;

it still doesn't achieve anything tho... also i think the error lies in the naming of your variable in CenterPrefab(), since the parameter currentPrefab and the public variable currentPrefab have the same name...try changing it

avatar image wattse13 rage_co · Jul 26, 2021 at 03:32 PM 1
Share

Thanks for the reply! I have it set up that way, mostly because I don't actually know what I'm doing. I made both they changes you suggested, but I'm still getting the same error. There is a very good chance that my whole approach to this problem is flawed.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by rage_co · Jul 27, 2021 at 04:09 AM

Ah! I just paid attention to what you said in the paras after the code (i was late last night, sorry i didn't pay more attention then)...so the first solution i suggested is wrong, you can roll it back, keep the variable names solution tho, since i didn't understand the jist of the problem, the error lies in the fact that you cannot call a function without providing the adequate parameters.

The correct approach would be to use another parameterless function to call the CenterPrefab() function

 public void CallCP()
 {
   GameObject prefabReturn = GetCurrentPrefab();
   CenterPrefab(prefabReturn);
 }

and remove the

 currentPrefab = GetCurrentPrefab(currentPrefab);

line from CenterPrefab() altogether. Also you need to remove the GameObject parameter from the GetCurrentPrefab function, instead just replace it's contents entirely with

 {
   Debug.Log("");
   return(currentPrefab);
 }

Also SetCurrentPrefab is kind of both useless since it only calls GetCurrentPrefab with the same parameter and i don't think it does the job it's intended to...so to correct it

 public void SetCurrentPrefab(GameObject myClickedPrefab)
 {
    currentPrefab = myClickedPrefab;
 }

and pass the myClickedPrefab parameter through the inspector (seeing that you aren't using raycasts directly on objects and instead buttons to do the trick).....and i think this should do....Hope this helps!

Comment
wattse13

People who like this

1 Show 2 · 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 wattse13 · Jul 27, 2021 at 08:29 AM 1
Share

It works! In addition to your suggestions, I had to declare prefabReturn as a GameObject variable. After that I took the empty GameObject that the InspectMenuController script was attatched to and added it to the OnClick() method through the inspector. Thank you for your time and help! How can I mark you answer as correct? Edit: It also works when prefabReturn is replaced with currentPrefab.

avatar image rage_co wattse13 · Jul 27, 2021 at 09:06 AM 0
Share

Oops, sorry about not declaring prefabReturn as an GameObject, i wrote the code right here..soo i couldn't check for errors...i will edit that into the comment and convert it into an answer...then you can accept it...again, glad that i could be of service.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

182 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Argument Exception when no arguments? Method Arguments are Incompatible 0 Answers

Button OnClick function? 1 Answer

OnClick Functions Not Appearing,OnClick Not Showing Public Functions 1 Answer

Function Not Appearing In OnClick Editor 13 Answers

Unity 4.6 Button OnClick's Functions not showing. 9 Answers


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