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 Ochreous · Apr 29, 2015 at 04:31 AM · c#uifunctionassignexecute

C# how to Access UI Assigned to Function

I would like to get a reference to the UI that is assigned and executes a function. Like a button for example. However when Debug.Log(this.gameObject.name); occurs it refers to the gameobject the script is attached to and not the UI. Is this possible or do I have to give my function parameters in order to access the UI element that executes the function?

Comment
Add comment · 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 KdRWaylander · Apr 29, 2015 at 06:49 AM 0
Share

Well, 'this' keyword means what it means ^^ It means 'on the gameobject the script is attached', you usually don't use it because it is implicit when you call stuff like 'transform.position'.

You want, when you call a function (via a button), that the script that is readed does whatever you want it to do + throwing a debug.log of the name of the object it interacts with ? Do I understand well what you want ?

avatar image fafase · Apr 29, 2015 at 07:12 AM 0
Share

I think he is after the same principle you get on event listener in .NET

 public void $$anonymous$$ethod(object caller, EventArgs e){}

where the first parameter is the caller.

But since you cannot pass object that would be Object.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Thomas Train · Apr 30, 2015 at 07:13 AM

you can get a reference via GameObject which your UI scirpt attached.

check the below web page first.

http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Comment
Add comment · 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
0

Answer by fafase · Apr 29, 2015 at 06:45 AM

 public void Method(Object obj)
 {
     Debug.Log(obj.name);
 }


You can pass anything that is an Object and anything is an Object in Unity (ok not totally but GameObject and MonoBehaviour). So you can pass the calling Button or Canvas or GameObject holding any of those.

Comment
Add comment · 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
0

Answer by Soiltest · Apr 29, 2015 at 02:21 PM

Of course like this you have to pass the InputField as a parameter, if you only need this method to assign a value to a string you could just pass the InputField.text as a parameter.

To make your setup work, you have to assign "Scripts" to an object and drag that object in the editor (where you dragged Scripts). You also have to modify slightly your functions, like this:

using UnityEngine.UI; [SerializeField] private InputField _userField; [SerializeField] private InputField _passField;

// Other code.

public void UserInput() { string username = _userField.text; // Code that uses the username variable. }

public void PassInput () { string password = _passField.text; // Code that uses the password variable. } This way you can actually drag the InputFields from the scene hierarchy to the "Scripts" object directly in the editor.

Hope this helps! Regards: Soil Testing Services

Comment
Add comment · 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

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

20 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

Related Questions

Canvas Button OnClick() function arguments 1 Answer

check if part of function is executed in another script 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why will my button not let me run a function? 4 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