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.
Related
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 have a script creating UI buttons in Unity. It creates instances of a prefab, which has also custom script components in it. I'd like to instantiate a new copy and immediately access values members/fields of the newly created object's scripts:
turretButtons.Add(Instantiate(buttonProto, gameObject.transform));
turretButtons[turretButtons.Count - 1].image.sprite = turretIcon;
turretButtons[turretButtons.Count - 1].GetComponent<DetailsWindowController>().turretDefinition = turretDef;
The first line creates the new instance, the second change the icon, both work perfectly. The third however, in which I try to access the DetailsWindowController script/class's turretDefinition public member throws "NullReferenceException: Object reference not set to an instance of an object". What am I missing?
The component you are looking for is probably not attached to exactly the same GameObject the Button component is attached to.
You should use GetComponentInChildren in order to always look for the component recursive downwards inside of the button's hierachy. Also note the true parameter which is required to find components on disabled children. This might be usefull in the case the button is spawned disabled.
var newButton = Instantiate(buttonProto, gameObject.transform);
newButton.image.sprite = turretIcon;
newButton.GetComponentInChildren<DetailsWindowController>(true).turretDefinition = turretDef;
turretButtons.Add(newButton);
Um.. First thing's first. I must let you know that I don't have core knowledge in C# scripting at all.
Alrighty, so here's my case. :)
Let's say I have a Script named AIEnemy with this snippet:
public delegate void CallbackOnEnemyAttack(int attackDamage);
public static event CallbackOnEnemyAttack OnEnemyAttack;
And I have a GameManager Script that sits on a one-and-only empty GameObject.
The case is... the AIEnemy Script is attached to lots of other GameObjects in the Scene. And I'm left wondering if the delegate AND the event that I declared in the script are of single instance or will be different per AIEnemy component attached to every GameObject.
I'm kind of a neat freak when it comes to game architecture even though I'm a noob. I'm super interested in delegates/events in Unity to design the architecture of my scripts better since I really don't like how Unity doesn't expose public instances of Interfaces in the Inspector that I have to do a "hacky" way to get around it by casting/tightly coupling my scripts together.
The idea of making an event static in Unity is so that you can easily subscribe to it without needing an instance of it. This should be used in a script with one instance and this means it must be attached to one GameObject only.
That delegate and event code should be put in a script called EventManager or somthing similar. This EventManager script is a script that is attached to one empty GameObject. Your enemy and other scripts can subscribe to this EvenetManager when they are spawned and also receive notification when there is an event.
What happens when you attach this multiple GameObjects and now it has
multiple instances?
If you trigger and event, each script you attached to a GameObject will send their own event. If this is attached to 500 enemies and you trigger event, all the 500 enemies will send an event to the subscriber.
In most cases, it is better to make these variables private and expose a function that can be used to subscribe and trigger events on them.
This has been done already. Here is a complete example of EventSystem from Unity that uses Unity's UnityEvent which is slow. Here is the ported version I made that uses delegate and should be used if possible.
Subscribing to an event:
EventManager.StartListening("Spawn", SomeOtherFunction);
Un-Subscribing to an event:
EventManager.StopListening("Spawn", SomeOtherFunction);
Triggering an event:
EventManager.TriggerEvent("Spawn");
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.
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;
}