I am new to Unity and C# coding and I am follow a tutorial on Youtube.
This is our code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] Vector2 jumpVelocity;
void Update()
{
if(Input.GetButtonDown("Fire1")) {
GetComponent<Rigidbody2D>().velocity = jumpVelocity;
}
}
}
When the tutorial instructor presses play on unity, It jumps a bit and gives him no error.
But I receive the error: ArgumentException Input Button Fire1 is not setup. To change the input setting use: Edit-> Settings -> Input
Most of the solutions say that I must capitalize Fire1 and in the code I did and I still receive the error.
Any help would be appreciated. Thanks
From your post, you don't mention having followed the error's suggested resolution. That being the case, we should make sure that "Fire1" is actually defined. To do so, in the Unity Editor, select the "Edit" menu option, then select "Project Settings" (possibly just "Settings" on older Unity versions). In the popup window, look down and find "Input Manager".
Make sure that there's an entry for "Fire1" in the 'Axes' list.
It should look like this:
You can see here all the options for what an input is. You'll also notice that in my setup, "Fire1" is the left ctrl or the left mouse button (mouse 0). You can either select an input definition that's already setup, or create "Fire1" to mimic what's in the image.
Note: as per the comments, be wary that any leading or trailing spaces aren't trimmed and can cause the issue exhibited.
Related
So I'm building a virtual gallery where the user is able to walk around and see pictures.
Now when he clicks on a picture, I want that picture to pop up take up the screen and also display some metadata about the picture, maybe read a text file related to the picture? How do I do this? I'm very new to Unity so any help will be greatly appreciated.
I'm using the below code to detect the gameobjects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class ObjectClicker : MonoBehaviour
{
public LayerMask interactableLayermask = 6;
UnityEvent onInteract;
void Start()
{
}
void Update()
{
RaycastHit hit;
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2, interactableLayermask))
{
Debug.Log(hit.collider.name);
}
}
}
```[I want to click on them and that image pops up and takes up the screen and displays some metadata about it.][1]
[1]: https://i.stack.imgur.com/skz8H.png
You can create a Panel (Unity UI), add some elements to it (a button, image, etc) and then create a UIManager object and script.
Now, you can create the methods for that UIManager:
bool showArtInfo (int index)
bool isPanelActive ()
void closePanel ()
And references for the Panel and its picture, text in the script.
You can add an array of images and text to this object. Note indices matter! (or you can use another type of collection instead of array)
Finally, to each object to which you are assigning this ObjectClicker script, you can add a unique constant index so it can indicate who it is to the UIManager. Also add a reference field for UIManager so you can bind the UI Manager script to this guy (you'll have to bind for every gallery object you have - there are better ways if you have many objects, but this is simple).
Now, inside the Update() in ObjectClicker, you can add a call like:
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2, interactableLayermask)) {
if (! uiManager.isPanelActive ()) {
showArtInfo (myIndex);
}
}
How to implement showArtInfo?
You have to set the image from the array according to index, set text and enable the panel. You can also animate it to make it look nice.
How to implement isPanelActive?
Report whether it is enabled or not, if animating note this might not be enough - experiment to see what is nice.
How to set image?
I don't recall the details, but perhaps this might be useful: How do I change the source image of a Unity image component?
Also note, the Unity UI Panel does not need to be flat 2D, it can also be rendered in World Space, so it moves around with your 3D elements!
There might be many better ways to do this! If working with many objects, you can store details on the objects themselves using custom components, and GetComponent on the gameobject when hit, and pass it along to the UIManager - it will be easier to manage.
I'm exploring a bit in Unity and C#, I searched everywhere for a solution without luck.
Problem:
In Scene A, I have an object button called "Button_2" -> it's hidden.
In Scene B, I have another button called "Button_1", and this one, must unhide "Button_2", in the other scene "Scene A"
How can I achieve this, if possible of course?
Thank you.
EDIT 1:
I tried the following:
Used DontDestroyOnLoad. Basically can't use it on a single button. Unity doesnt allow it. So, I had to do it on the canvas, but when I switched to another scene, was a huge mess with both scenes mixed.
Tried Yuris solution and suggestions and didn't work. Scenes just don't interact with each other at all.
Tried to combine both of above with the prefab concept Yuris suggested aswell and still no luck.
I will keep doing my best, but my knowledge is very limited. I'll be back with more feedback if I make progress.
EDIT 2:
Made many experiments with a non monobehaviour script file and class with only variables to be accessed by a script file that is linked to gameobjects in the diferent scenes.
I wasnt hable to access the variables on the non monobehaviour file. Looks like unity doesn't like non monobehaviour and didn't work.
You can make the button a prefab and assign it on a script in the Scene B, and then when you load scene A again, it will be active.
Remember that if you use that prefab anywhere else, it will be active there too.
Example:
Example.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
public GameObject button;
// Start is called before the first frame update
void Awake()
{
button.SetActive(true);
}
}
The following code locks the cursor only when I left click my mouse after pressing Escape Key. Can anyone suggest me a way to overcome this problem?
public class lockCursor : MonoBehaviour
{
public bool locked = true;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (locked == false)
{
Debug.Log("locked");
Cursor.lockState = CursorLockMode.Locked;
locked = true;
}
else
{
Debug.Log("Unlocked");
Cursor.lockState = CursorLockMode.None;
locked = false;
}
}
}
}
As said in the API for Cursor.lockState
In the Editor the cursor is automatically reset when escape is pressed, or on switching applications.
Due to escape having a special role itself within the Unity Editor the GameView loses focus when pressing it.
There is not really a (trivial) way around this since it would require to intercept they keypress before the Unity Editor itself gets the chance to handle that escape press. (Which would always happen before it is passed on to the player -> your code.)
What you can do however would be simply use a different KeyCode only for the editor and escape only in a build like e.g.
#if UNITY_EDITOR
if(Input.GetKeyDown(KeyCode.E))
#else
if(Input.GetKeyDown(KeyCode.Escape))
#endif
{
...
You can replace the E with any other KeyCode you wish to use to "simulate the escape key" only within the Unity Editor itself. (Also see Platform dependent compilation)
As derHugo mentioned in his comment, your issue lies in the API for Cursor.lockState where it says:
"In the Editor the cursor is automatically reset when escape is
pressed, or on switching applications. In the Standalone Player you
have full control over the mouse cursor, but switching applications
still resets the cursor."
When you go to press Escape, it removes you from the application/Game Window. This is why it'll appear like your cursor is no longer locked. This is also why it goes into it's locked state as soon as you click back into the game. The same thing can be demonstrated by using your code, starting the game and then switching into another window on another monitor. Then move your mouse back over to the screen where the Game Window is. It'll appear as though it is no longer in a locked state... that is until you click in the game and it recognizes the cursor again.
To solve your problem, I would recommend using any key other than KeyCode.Escape. Maybe KeyCode.Space like the API shows.
Also, to further demonstrate the issue here, once you switch the code to use KeyCode.Space, press Space to lock the cursor and then try pressing Escape. Again you'll see that it appears to be unlocked until you press the Left Mouse Button in the Game Window again. This is because the Game thinks that you are no longer in the Game Window once you press Escape.
Hope this helps!
The default buttons for grab is "Axis1D.PrimaryHandTrigger" and for the button click is "Button.One". Although these are the default ones, I would like to change the settings so it can take in different inputs.
I could not find where and how the input is added to these OVR touch controllers.
Check this official docs by unity for VR inputs with their Unity Axes IDs.
Go to the Project Settings and select Input. There you can change one of the input for your custom VR input as shown in the picture below:
Also change the Axis from Y axis to 11th axis (joystick) and untick Invert. See the picture below:
Now you can call them in your script like this example:
void Update()
{
if(Input.GetAxis("Vertical") == 1)
{
//Do something
}
}
When I create a button in unity3d 5.4 the button just clicks automatic when i press play in the editor. I haven't been using unity before, so I can't say if it would happen in other versions.
First i create a script, then I attatch it to a empty gameobject. Then I press the little + sign on the OnClick() in the button properties, so I can add the gameobject with the script attached.
I have screenshots of button properties, eventSystem, Canvas, gameobject and the Script.
If anyone know what i have done wrong, please let me know. Thanks in advance.
The screenshots are in this post, since I cant post 5 images in stackoverflow:
http://forum.unity3d.com/threads/unity-5-4-button-auto-clicks.426526/
When you press play in editor, Button is NOT pressed at all. Its just that you have Debug.Log("Clicked!") in Start() method. Start() is called by Unity automatically when you run the application. It has nothing to do with button click. You need to register OnClick() listener method so it will be called when you press the button.
Have a look at this tutorial : https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button
EDIT :
Please learn more about scripting here: https://unity3d.com/learn/tutorials/topics/scripting