Well,I am working on my graduation project with unity 3d pro v4.5.0 ..I was using the normal method OnGUI() in my scripts but now i want to make a better interface for my project and i didn't anderstand how can i replace for example these lines in my "Database" Script:
if (GUILayout.Button ("Add to database"))
{
// Insert the data
InsertRow (FirstName,LastName);
// And update the readout of the database
databaseData = ReadFullTable ();
}
With just calling a Button1_click for example from my iGUI Class specially that every thing related to interface and buttons exists in OnGUI() method so how can i change all of that in my "Database" Script..i did anderstand how to make a button in iGUI that LOAD a scene or set an other function in the same iGUI Class but i just want to make buttons and iGUI field texts in iGUI MonoBehaviour Class and call them later whenever i need in my pricipal script "Database" if it is possible.Please am so in need for help. Thank you in advance.
You need do this:
create a public method in any script.
add the script to a GameObject.
Click the button and add the object with the script in the method
OnClick located in the inspector.
Choose your script and the public method where said No function.
Boala!!, when you click, your method run.
Related
I have this problem. I wrote a script with a method when interacting with which the player exits the game, but when I wanted to bind it to the button, and I realised that it didn’t work . In the OnClick() component, I attached my script, but for some reason the method is not attached.How can I attach my method?
Create an empty GameObject, attach your credits.cs to it then assign this gameobject to the OnClick(), then Credits > (Your method).
Make sure your method is public.
I'm sure that I am tangled in my own code so it would be easy I bet to help me out of it I would really appreciate someone throwing me a line.
Can you attached a script to a button and then call a method in that script using that button's onClick in the unity inspector?
just so you know, you create a specific 'button' in unity, not a GameObject with a sprite, so that it has a built in unity function where you write a very short piece of code:
public void FunctionName()
{
Do(what_youWant.here)
}
and put the script on the actual button, then have a look at the button's components list, and you'll see a place for functions; there you select (from the dropdown) your function from the code.
I want to select a gameobject at run time in unity and move it using an UI button.
for example if there are three cubes in the scene i want to select one cube and one that I clicked will move once I click the UI button .
I have no idea how to pass the value of the selected object to the button click event.If some one can show it using a sample code it would be great.
You can set a tag on the object and then use the GameObject.FindGameObjectsWithTag() function to get that object.
Here is the documentation for the function: https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
Use this:
var obj = Selection.activeObject;
When working with objects that are primarily in a scene, it is strongly recommended to use below code instead:
var transform = Selection.activeTransform;
Refer to official doc here.
How can I achieve this? A function can take only 1 argument in Unity for an event.
There are two different input fields in the same canvas which I'd like to compare.
I'm assuming this can be done using object as argument but I don't know which object I can pass. I tried dragging the canvas and input field gameObject(I'm guessing that's what it's called) to the object parameter field of my selected function but they don't get accepted.
This is where I want to attach the script:-
InputFields are not GameObject. They are objects of UI.InputField class. So in your script import UI with
#using UnityEngine.UI;
Then create 2 InputField object.
public InputField inp1;
public InputField inp2;
then drag your InputFields to them. then you can compare their text like that:
if( inp1.text == inp2.text) {}
Edit: You shouldnt attach a script to there, because the variables may change dynamically. attach this script I have wroten to an empty game object, add new snippet on below to script, and then attach this empty game object to event field that you showed. Then you should select the function will be called when edit ended. For this, create two different function in script for each input field as:
//Call this on EndEdit for inputField1
public void InpField1EndEdit()
{
//compare input fields here or make what you want
}
//Call this on EndEdit for inputField2
public void InpField2EndEdit()
{
//compare input fields here or make what you want
}
I've been struggling with what appears to be a simple task for the past few hours. I have been successfully able to turn a GameObject's guiTexture on an off using GameObject.guiTexture.enabled as I have read in other similar questions. I do this in a script that is attached to the GameObject I wish to toggle.
The problem arises when I try to preform this same command from another script that is attached to an empty game object in my scene. When I interact with the button, simply nothing happens.
To give this some context, I am trying to create functionality to switch menus on the screen. When a user taps a certain UI button, I want to call unloadMenu() so I can wipe everything off the screen and draw the textures that belong to the appropriate menu.
//When the user lifts their finger
void OnTouchEnded ()
{
switch (this.gameObject.name)
{
//If they have selected the quiz button
case "QuizButton":
//this.guiTexture.enabled = false;
//Call the unloadMenu function which will clear the GUITextures
menuMgr.unloadMenu();
break;
The commented out section is what works fine. But now, I wish to perform this code in a function that is located in another script - menuMgr.unloadMenu()
This is what the MenuManager script looks like:
public GameObject quizButton;
void unloadMenu()
{
quizButton.guiTexture.enabled = false;
}
I have ensured to drag the quizButton gameobject from my scene into the inspector and connect it properly with the variable I have created yet nothing happens when this function is called. Does anyone have any idea why? Thanks in advanced.
How you declare MenuManager creates a new script while the reference to quizButton is in your MenuManager GameObject. That is why your menuMgr.UnloadMenu didn't do anything.
I have created something similar to yours and I found it worked fine :
Quizbutton Script:
public GameObject menuMgr; // reference to the empty game object (menu manager)
void OnMouseDown() // In your case, this is OnTouchEnded
{
// get the script of menu manager here
MenuMgrScript menuscript = menuMgr.GetComponent<MenuMgrScript>();
// and execute the function you want
menuscript.UnloadMenu();
}
Menu Manager Script:
// this script is exactly the same as yours, nothing's wrong here
// oh except add public to function UnloadMenu, so it can be called
// in the quizbutton script
public GameObject quizButton;
public void UnloadMenu() {
quizButton.guiTexture.enabled = false;
}