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.
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 create a set of prefabs at runtime via script. They are held in an array called newObj. Each one has some text UIs and some buttons, which I retrieve with GetComponentsInChildren. When a user clicks the first button in the prefab, I want to run a function that changes the text of the button and highlights that button.
Everything is working except the button doesn't highlight.
public void SelectPlayer(int rowSelected)
{
var buttons = newObj[rowSelected].GetComponentsInChildren<Button>();
var texts = newObj[rowSelected].GetComponentsInChildren<Text>();
texts[0].text = "1";
buttons[0].Select();
buttons[0].OnDeselect(null);
}
Oops. I'm still new to Unity and forgot that Unity sets the default highlight color to white (for some reason). Once I changed it for my prefab using the editor, all was well.
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);
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 am trying to create an game board by using Unity and NGUI and everything is working ok now. I am using the DragNDrop script on my dragable object and on my fields on the board I am using UIDragDropContainer. But, is there a way I can tell my dragable object which field the object is over or attaching it self to?
I have scouted the net, but have not found an solution.
Any help is appreciated :-)
Use the UICamera.hoveredObject to get the last gameObject (with a collider) that the mouse or touch was last over.
In one of my projects I came across a same issue. What I did was inherit the NGUI's UIDragDropItem script and created a delegate to tell me about the object. I have also added the source:
public class Custom_DragDropItem : UIDragDropItem {
public delegate void DragDropDelegate(GameObject TargetObject);
public DragDropDelegate onDragDrop;
}
All you have to do is use this script instead of UIDragDropItem on your draggable items in the inspector and register to the delegate onDragDrop and it will pass you the target object on which the item is dropped.