C# Charakter kontrol - c#

hello as shown in the picture when the game started in the unity project healtUI I need to show the path Raw_Hp. How can I do th

Did you try dragging and dropping the Raw_HP element into your Healt UI place in your Hp Bar script?
The Raw_HP is not instantiated so you can just simply drag it there.
AN EDIT TO YOUR COMMENT
So I see you instantiate/spawn your character object that has the Hp Bar script
(Althought I can see many better ways to handle this situation)
But if you want to continue with this thing, just add a GameTag to your hpbar like "GUI_HpBar" and then in your Hp Bar script do
void Start() {
HealtUI = GameObject.FindGameObjectWithTag("GUI_HpBar")
}
This will automatically find the desired HpBar and link it.

Related

How to make a picture appear at the center of the player camera when an object is clicked?

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.

Creating a monitor in Unity

I have a prefab of a submarine I made in blender. In this submarine there is a "altitude" screen which is basically a cube I scaled and painted black.
I can easly get the altitude of my submarine by doing sub.transform.position.y.
Now I want to put a "Text" over the screen with the altitude I am getting.
My question is : What is the best way to do it ?
In an Ideal world I would have a function like
TextObject putTextOnTop(GameObject the_panel)
that would generate a textObject that I would then update each frame with my value.
Thanks
There are multiple solutions for this.
There is an old 3D Text component in unity. This can do what you want. The Problem with this is, that you have only limited styling options.
A Better way is to use a WorldSpaceCanvas.
If you use this option, you have all TextMeshPro styling options.
Here is a video:
https://www.youtube.com/watch?v=GuWEXBeHEy8
The video is not very good, but i can't find a better one.
If you have any Questions left, feel free to ask
You can put the Text GameObject to be the child of the screen (Cube GameObject), there are 2 ways for this:
Set directly in Hierarchy
Create a prefab TextObject and save it in Resources with path: Resources/UI/TextObject. Then in the code, instance and add it as a child of the Cube using the following code:
GameObject textObject = Instantiate(Resources.Load("/UI/TextObject") as GameObject);
textObject.transform.SetParent(cube.transform, false);
Then, in the Update function you add the following code:
void Update()
{
TextObject.GetComponent<Text>().text = sub.transform.position.y.ToString();
}
Hope it can help you!

Unity C# - Hide / Unhide a Button in a different scene

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);
}
}

Strange plane behavior in Unity3D while changing scene

I am a beginner with Unity3D.
Until now I have learned a few things.
For example, create a simple 2D menu, switch from one scene to another with an appropriate C# script and associate the scripts with the objects.
The problem I pose here is about the behavior (strange to me) of the white panel behind the menu when I change the scene.
I have two scenes, the first which is the menu and a second one is the actual game.
I used UI elements to create the first one, while I used 3D objects to make the second one.
So, what happens?
It happens that when you step into the second scene by clicking on the green button the background becomes brown. If, on the other hand, I run the second scene individually without going through the first one (launching the scene directly), the background remains white.
See this short video for further details.
Why?
Listed below are some images, hoping they can help me understand my problem.
The first scene (dev mode):
The first scene (running):
The second scene (dev mode):
The second scene (running directly):
The second scene (running after clicking the green button "Comincia"):
The script I use to change the scene (linked to the green button):
using UnityEngine.SceneManagement;
using UnityEngine;
public class GameScene : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PlayNow () {
SceneManager.LoadScene("Game", LoadSceneMode.Single);
}
}
Update: I have updated the information on the application. I added a new screenshot where you can see the camera settings. Meanwhile, I changed some things, but the problem still remains. I also noticed that this problem only happens when I switch to the Game scene, when I switch to other scenes instead it does not.
Added a new screenshot showing the inspector of the Main Camera object:
I was able to understand where the problem was: I made an improper use of the Plane object (highlighted in the screenshot). By removing it, now it shows correctly the solid color white background as set on the Main Camera.

Drawing GUI stuff in scene view Unity3d

is there anyway to draw stuff on scene view without calling OnSceneGUI?
or is there a way to get OnSceneGUI to get called even if the object with the script attached is not selected?
Edit: Looks like I wasn't explicit enough on what I was asking for so here's a little update:
I have a whole bunch of controls shown as GUI objects on my Game Scene that are used by the game designers to more easily test the game. Since these tools are for development use rather than deployment, I wanted to move these to the scene window instead.
Unfortunately I am only able to display these GUI tools if the object that contains the script with the "OnSceneGUI" method is selected. Instead I want them to be displayed whenever the game is running and regardless of the selected object.
I have found the solution to my issue.
If you want your GUI stuff to always be displayed on the scene window you can do the following:
public class MyClass{
static MyStaticConstructor() {
SceneView.onSceneGUIDelegate += OnScene;
}
static void OnScene(SceneView sceneView) {
// Draw GUI stuff here for Scene window display
}
}
This code will run as soon as you press play and will draw whatever you wish on your scene window.
Hope it will help others!
There are many ways to use GUI,
The easiest but least efficient way is using the GUI class on OnGUI()
GUI.Label(new Rect(10,10,10,10), "This is a label");
GUI.Label(new Rect(10,10,10,10), "Your Texture2D here");
Any active monobehaviour will run OnGUI() if its defined. So it can be attached to any gameObject. You can create an empty gameObject in the scene and call it "GuiGameObject" for example and attach the script there. That way it wont be mixed in with your gameplay script.
There are also GUI textures --> More Info on GUITexture
Also I recommend checking out nGUI
Edit:
For OnSceneGUI You can try Editor.Repaint, you can use it to make sure that the inspector updates changes made inside of OnSceneGUI

Categories