I have a scene with 1 moving object and a canvas with a button play
and when I run the scene the object moves because it has a certain Script assigned with a speed and so on... But the button play
is in the center.
What I want is for everything to be still and when I hit the button, the button it will be with disappears button.setActive(false)
and the object moves.
I'm trying to access them scripts
to do it but it won't let me refer to them and I don't want to start putting them static
or not static
because it would throw everything off, how can I do it so that once I hit the play button on the canvas, the other is executed? Within the same scene everything.
A possible solution would be to cancel 1 script
and until you give "play" not execute that script (the movement of the object), but how do you do that?
Script to move the object:
public class MoveBall_ : MonoBehaviour {
public float velocidad = 10f;
private void Update()
{
transform.Translate(new Vector3(1f, 0f, 0f) * velocidad * Time.deltaTime);
}
}
What I need is for that to velocidad
be 0 before clicking on the button play
on the canvas, and when clicking on the play
secondary canvas that I have placed (which I think I know how to do) disappears and the ball starts to move just like that Script .
This can be done in many ways, but since it mentions the use of a button that is on a canvas, we will use this approach:
We change what you have as speed to
velocidad = 0f
and create a method that will take care of setting the speed tovelocidad = 10f
.Then on your canvas select the button look for the "component"
Button (Script)
if it does not appear for any reason add it from the list of componentsAdd Component
look for a part within the Script that says something like:Click on +.
You will be shown something like the following:
Now click on the place that is marked with a
1
a box will open somewhere where you can see a tab with the name "Assets" and another with the name "Scene" if the object that has the script is in the scene look for it in that tab and if it is in assets then look for it there, and select it.After the above, in the place that is marked with a
2
select and a list will appear with the "components" of the object, look for the script that has the speed, I imagine that it will be calledMoveBall_
when hovering over the script, a list will appear with the variable methods ect accessible for that script, select the one with the name ofPlay()
because it is the one we created in the script.Now clicking on the button will "call" that method.
Note: the above can also be used for variables, methods with parameters, etc. The only difference is that to the right of what is with a one, a field will appear to fill in, in which you will put what the method or variable requires.
PS: Yes, I could directly use the variable to modify it without using any method, but I think that the previous way was better understood, and it also covered the part of using methods as well.
The solution was simply in the Script to put speed = 0 but you had to do some maneuvers with the
static
yfloat