Rotate cube on button click in Unity3D - button

I have created one cube. I want to rotate that cube on button click.
I created script for rotating cube and another script for button click.
Now how can I make that cube rotate on this button click.
Code for Button:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ButtonInput1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
Button button=null;
void Rotate()
{
a++;
button.onClick.AddListener (delegate() { PlayerController.CubeRotate ();});
}
}
Code for RotateCube:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Use this for initialization
// Update is called once per frame
int a=1;
public void CubeRotate () {
if (Input.GetButton("Button1")) {
a++;
//transform.Rotate ( new Vector3(15,30,45) * Time.deltaTime);
}
if (a % 2 == 0) {
transform.Rotate ( new Vector3(15,30,45) * Time.deltaTime);
}
}
}
I just need to know, how to call cube's script on this button click. Is there any other way to achieve this?

Thats funny, i actually just answered this question worded exactly like yours asked by another person. Rotate object continuously on button click Unity3D

Related

How to move and rotate game object in the direction of movement,How to rotate and move gameObject in the direction of movement

I'm doing a top-down shooter in Unity 2D and I'm currently trying to make a fire point, but when a character changes direction of movement, the fire point stays in the same position, but I want it to move and rotate. Sadly i have no idea how to do it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
public Animator animator;
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if(movement != Vector2.zero)
{
animator.SetFloat("Horizontal", movement.x);
}
animator.SetFloat("Speed", movement.sqrMagnitude);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
}
}
enter image description here
enter image description here
I want it to be like the first picture, but on the opposite side.
enter image description here
Something like this (I rotated it in photoshop)

Detecting which UI button was pressed within canvas?

I have like 10 buttons on my UI and I gotta check which one was touched. I was using the following logic and it was working fine, but now I am getting this error for some reason:
NullReferenceException: Object reference not set to an instance of an object
DetectButton.Start () (at Assets/Scripts/DetectButton.cs:14)
Any ideas what could be going on? Here is my code (attached to the canvas), and I am using Unity version 5.1.0f3. If you need any other info I will gladly provide, thanks in advance
void Start()
{
this.GetComponent<Button>().onClick.AddListener(() =>
{
if (this.name == "btnJogadores2")
{
print ("2 jogadores");
jogadores = 2;
}
//QuantidadeJogadores(this.name);
//QuantidadePartidas(this.name);
});
}
You don't have to all this the way you are doing.
An Easier and good practice would be to create 10 separate GameObjects for each button inside your canvas. and then create a single script with 10 separate functions for all those buttons in it. Attach that script to you canvas. and then on the button GameObject select the script on the desired function. Sample below
void Start() { }
void Update() { }
public void button1()
{
Debug.Log("Button3");
}
public void button2()
{
Debug.Log("Button1");
}
public void button3()
{
Debug.Log("Button3");
}
NOTE: button1, button2 and button3 are the functions for 3 separate buttons
Then inside your unity Inspector:
Select your script with you button functions.
Assign you desired method to you button.
After this run your scene and your button will call the assigned methods properly.
Code is not tested, but it should get you started to get all the Buttons.
void Start() {
var buttons = this.GetComponents<Button> ();
foreach(var button in buttons) {
button.onClick.AddListener(() = > {
if (this.name == "btnJogadores2") {
print("2 jogadores");
jogadores = 2;
}
//QuantidadeJogadores(this.name);
//QuantidadePartidas(this.name);
});
}
}
Actually it will be hard to distinguish between the buttons.
The more practical aproach would be to make 10 GameObjects (Child of the Canvas) and attach your Script to everyone of them.

TranslateTransition in JavaFX does nothing

I'm trying to use a TranslateTransition object in JavaFX to move an onscreen object in a LOGO program I am building. I have an onscreen TurtleDisplay object, which extends ImageView, and this is what I'm trying to move. The code to move it is here:
public void drawTurtle(TurtleData currentData) {
TurtleImage inList = getTurtleImage(currentData);
if (inList==null) {
TurtleImage temp = new TurtleImage(currentData.getX(),
currentData.getY(), currentData.getHeading(), turtleImage);
myTurtlesGroup.getChildren().add(temp);
myTurtlesList.add(temp);
}
else {
TranslateTransition tt = new TranslateTransition(Duration.seconds(3),inList);
tt.setFromX(inList.getX());
tt.setFromY(inList.getY());
tt.setToX(inList.getX()+currentData.getX());
tt.setToY(inList.getY()+currentData.getY());
tt.setCycleCount(Timeline.INDEFINITE);
tt.play();
}
}
This code, which is part of the front end, is called from the back end via a Listener on an ObservableList. The backend contains this ObservableList of TurtleData objects that contain the information necessary to move a turtle on screen -- which turtle to move, the coordinate to move to, and the rotation of the turtle. The code to call this is here:
ObservableList<TurtleData> myTurtles = FXCollections
.observableArrayList();
myTurtles.addListener(new ListChangeListener<TurtleData>() {
#Override
public void onChanged(Change<? extends TurtleData> c) {
myDisplay.getSelectedWorkspace().getTV().clearTurtles();
while (c.next()) {
for (TurtleData addItem : c.getAddedSubList()) {
myDisplay.getSelectedWorkspace().getTV().drawTurtle(addItem);
}
}
}
});
I have stepped through with a debugger and ensured that this code is called -- specifically, the tt.play() line is run. Nothing moves on screen. Does anyone have any idea what is wrong? Do I need to setup an Animation Timeline? Thank you for any help!

How to print a value on the button when it is clicked?

I want print a value on the button when it is clicked.How it is possible?
[Update]
using UnityEngine;
using System.Collections;
public class tic : MonoBehaviour
{
void OnGUI() {
string value="";
if(GUI.Button(new Rect(10,10,50,50),value)) {
value="y";
print ("y");
GUI.Button(new Rect(10,10,50,50),value);
}
}
}
Adapting the Unity gui example you can modify the button text by storing that text in a variable and changing it when the button is clicked, like so:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public string ButtonText = "Click Me"
void OnGUI () {
if (GUI.Button (new Rect (10,10,150,100), ButtonText )) {
ButtonText = "Huzzah!";
}
}
}
The button will first read as "Click Me" then will change once to "Huzzah".
If you don't want to change the actual text in the button it gets a little tougher. You would need to create a label that sits over the button, I don't recommend going this route. It won't look nice and the label won't move w/ the button:
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour {
public bool DrawLabel = false;
public string LabelText = "Huzzah"
void OnGUI () {
if (GUI.Button (new Rect (10,10,150,100), "Click Me")) {
DrawLabel = true;
}
if(DrawLabel){
// use the same rect parameters as you did to create the button
GUI.Label (new Rect (10, 10, 150,100), LabelText);
}
}
}
You can do like this;
var ControlsButton = GameObject.Find("ControlsButton");
ControlsButton.GetComponentInChildren<Text>().text = "Accelerometer"

How to manage Mouse clicks on Irregular Button shapes in Flex

In Flex, I am trying to design 3 buttons similar to the image uploaded at
http://www.freeimagehosting.net/uploads/f14d58b49e.jpg
The mouse over/click on image should work only on red colored area of the button.
How can I manage the Mouse clicks or Irregular Button shapes in Flex?
Thnx ... Atul
Check this out: flexlib > ImageMap.
Taken from stackOverflow
Use button skins based on a vector graphic (e.g., one made in Illustrator), save each state as a named symbol in the document, then export as SWF. Reference the skins as follows:
.stepButton {
upSkin: Embed(source="myfile.swf", symbol="StepButton");
downSkin: Embed(source="myfile.swf", symbol="StepButtonDown");
overSkin: Embed(source="myfile.swf", symbol="StepButtonOver");
disabledSkin: Embed(source="myfile.swf", symbol="StepButtonDisabled");
}
Flash will automatically determine the hit area from the visible portion. This example (not called "myfile.swf") is working for us right now in an application.
Create ArrowButtonsHolder class by inheriting from Canvas
Create 3 children classes also inherited from Canvas. For example LeftArrowButton, MiddleArrowButton, RightArrowButton
public class LeftArrowButton:Canvas {
protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
// draw your arrow here
// use graphics to do it
graphics.beginFill(0xFF0000);
graphics.lineStyle(1, 0x000000);
graphics.moveTo(0, 0);
graphics.lineTo(30, 0);
graphics.lineTo(50, 25);
graphics.lineTo(30, 50);
graphics.lineTo(0, 50);
graphics.lineTo(0, 0);
graphics.endFill();
}
}
You also can create general class ArrowButton and inherit another 3 from that class and override drawing function
Add this 3 child button object to ArrowButtonsHolder by overriding createChildren():void method
public class ArrowButtonsHolder:Canvas {
// ...
private var leftArrowButton:LeftArrowButton;
private var middleArrowButton:MiddleArrowButton;
private var rightArrowButton:RightArrowButton;
// ...
protected override function createChildren():void {
super();
// create buttons
leftArrowButton = new LeftArrowButton();
middleArrowButton = new LeftArrowButton();
rightArrowButton = new LeftArrowButton();
// add them to canvas
addChild(leftArrowButton);
addChild(middleArrowButton);
addChild(rightArrowButton);
// position these button by adjusting x, y
leftArrowButton.x = 0;
middleArrowButton.x = 50;
rightArrowButton.x = 100;
// assign event listeners
leftArrowButton.addEventListener(MouseEvent.CLICK, onLeftArrowButtonClick);
middleArrowButton.addEventListener(MouseEvent.CLICK, onMiddleArrowButtonClick);
rightArrowButton.addEventListener(MouseEvent.CLICK, onRightArrowButtonClick);
}
private onLeftArrowButtonClick(event:MouseEvent):void
{
trace("Left button clicked");
}
// .. etc for another 2 methods implemented here
}
PS: There might be tons of syntax mistakes in my code but you should get general idea how to do it

Resources