Basic scripting
I am trying to make a function that changes the x pos of an object up until it reaches 15f when it sets the position back to 0. Obviously I did something wrong. I am a beginner in this so i don't know what I am doing wrong. If anyone can help me please do! ^^ I am trying to acces this function from a button, it is something like changing tabs in 2d, I set the camera to follow a cube in backdround. I am trying to change the cube' position, so that the camera will smoothly follow the cube.
Answer by Jawchewa · May 26, 2017 at 10:30 PM
There are quite a few errors that I can see with your code. First of all, method names should be one word, so change "Clicked Right to either "ClickedRight" or "Clicked_Right".
Next, is that you need to get rid of the "new" keyword when creating newPos. Normally, new is created when you want to create a new instance of a C# object, but it isn't needed if you are just setting a variable to a position. Also, you probably don't need to know it for this, but new usually works in this structure : MyClass instance = new MyClass();
.
The next issue that I can see is that you are missing a closing curly brace for your ClickedRight function, before the final curly brace.
Thank YOU sooo much! It was probably easy for you but these things are pretty hard for me because I have no experience. I have another question i have to ask you. I haven't researched how to use buttons but i tought that if I assign the object to it i can access a function inside the script attached to it but I do not see it there. $$anonymous$$aybe you can see the problem. Thanks again for your help! ^^ Next time i will be more careful.
You're on the right track, as that is where it should show up. The main thing that you are missing is that your function need to be listed as public, for it to be visible in the editor. So change it from:
void ClickedRight() {
to
public void ClickedRight() {
Your answer
![](https://koobas.hobune.stream/wayback/20220612122847im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
activate button on triggerenter 0 Answers
Spawn prefab on UI button click 0 Answers
I want to press a Button and play a sound? 2 Answers
Unity Button Script 0 Answers