Sprites drawing over text in unity - c#

I am trying to display the score for my game, however, my background sprite is drawing right over it. I have tried assigning some type of layer tag but I can't find one for text. Everything I try, the text is always hidden under the sprite for my background (and any other sprite for that matter). Is there a way to assign the text to be drawn over everything, I have tried looking at tons of sites in hopes of a fix but I can't seem to find anything. Any help is extremely appreciated.

From the hierarchy creation menu, pick UI->Text
This will add you 3 Elements to the hierarchy: Canvas, and 2 child gameobjects which are Text and EventManager. These are all needed for the proccess.
The text gameobject is part of the UI system Unity has, means that if done correctly, it should show over all sprites.
Programmatically you can change text by simply saying:
myUItext.text = "Text that will show on UI!"
Manually you can change position, font and text using the hierarchy view.
For further details use this video from the official Unity tutorial

Related

How do I attach a text component to a 2D sprite?

My game has this system where the player is rewarded with hearts.
Whenever a heart appears, it's supposed to appear with a value on it (say, +5). I did some googling and everything said I need to attach a canvas to the sprite and then a text component to the canvas. However, this doesn't seem to do anything.
You just need the following structure
Sample Image settings: (Here you'll want to replace the "Source Image" with your heart sprite)
Sample Text settings: (Although I recommend using the TextMeshPro component instead)
Result:

Handy swap between mutiple buttons sprite (C#)

So i have a UI, when the player opens it, he can choose between multiple heroes (these heroes are buttons). The thing is, the hero can be locked, unlocked, or already selected, depending on different stuffs. So the image/sprite that the player see will obviously not be the same. So what is the most handy way of swapping the sprites of all heroes so i keep them up to date.
I thought that having different sprites (locked/unlocked) for each heroes would be too much, and maybe superimpose unlock sprite with an other one to create the lock image would do the trick. But how do i play around with it.
Thanks in advance, my code for now will not be usefull in anyway for what i ask
If you build your button like this in the hierachy:
Where the ButtonHero is the acutal button and the ButtonForeground is just an image, then you can put whatever you want in front of the hero image. This way you don't have to have multiple images of the hero with something in front.
So by changing the Foreground's Sprite, you get different results:
Default:
Unavailable:
Locked:
Likewise, if you put the hero image in the foreground, you can change the background to make it look selected:
So it's just a matter of swapping out the foreground/background image.

Gaze Over, display Tooltip in Unity with GoogleVR

Does any know or can possibly point me to some instructions or a github repository on how I can create a script where I have an object and in GoogleVr (Cardboard) if I was to gaze over an object, a tooltip would appear?
If anyone is familiar, in the Cardboard Demos under Under Arctic Journey > Learn, when you click on the fox, a tooltip appears to showcase that item along with like a brief description on it. I want to have something similar (maybe even the same thing) except just having a gaze over will automatically show it. Is this possible?
I want to have this done on multiple objects in my project so I want it created so I can easily substitute out text and whatnot.
Have a script with a reference to a World Space Canvas (WSC). The WSC will be your tooltip and be activated when you hover over the object and disabled when you don't.
You can set images and texts of the WSC through the inspector or through code if you make a reference to them.
The script should also always have its rotation set to face the player.
You can use the SetActive(bool) method to show or hide the WSC.
The UI system makes it easy to create UI that is positioned in the world among other 2D or 3D objects in the Scene.
Start by creating a UI element (such as an Image) if you don’t already have one in your scene by using GameObject > UI > Image. This will also create a Canvas for you.
Set the Canvas to World Space
Select your Canvas and change the Render Mode to World Space.
Now your Canvas is already positioned in the World and can be seen by all cameras if they are pointed at it, but it is probably huge compared to other objects in your Scene. We’ll get back to that.
https://docs.unity3d.com/Manual/HOWTO-UIWorldSpace.html

Unity GameObject position z is mysteriously changed after object is created in script

I have once again problems with Unity. I can't spear much code due the level of secrecy agreement in the game I'm working on but I ran to this one annoying problem when running my code.
SO, I'm creating GameObjects in runtime when entering a certain view in the game. I have created a so called Reader for our game story which reads text from database, splits it to paragraphs, creates GameObject around that paraghraph text (setting text components etc. to it) and then adds that GameObject and its text to a story panel that shows all the objects as a scrollable view inside masked panel.
Hopefully you can keep up with my explanations :D
Everything works fine all the way to the point that the paragraphs are appearing correctly underneath the parent object (the view panel for the texts) and is shown correctly in the scene view, but... the problem is that, although i have made several checks in update loops, Unity just keeps giving random z position to the GameObjects setting them to
position z = -350
which makes them out of the player view and quite hard to read :D
I have debugged many times the position of the GameObjects in update giving it out z position 0 in every frame. This clearly doesn't mach the value the objects have in editor view during runtime...
Has anyone ran in to this kind of problem?
ps. I have treid to close and open Unity, load things again, nothing works.
oh and one thing to mention too is that to stretch the text to fit the size of text content and parent panel width I have used these as examples:
"Unity UI Tutorial - How to make a scrollable list" by rachetandclank3
Hmm... I somehow managed to fix this problem when I changed this:
this.paragraphObject.transform.localPosition.Set(
this.paragraphObject.transform.position.x,
this.paragraphObject.transform.position.y,0f);
to using this instead:
Vector3 newPosition = new Vector3(
this.paragraphObject.transform.position.x,
this.paragraphObject.transform.position.y, 0f);
this.paragraphObject.transform.localPosition = newPosition;
hopefully this helps others that also struggles with this kind of problem.
Source to the solutions: GameObject position.Set() not working
in Vector3 at z position try to change with gameObject.transform.position.z,
replace gameObject with the object name which position is changed mysteriously.

VR score text display

I am trying to make a VR game with google cardboard in unity. However we can not find a way to display score text right in front of the player. However when I add 2D text it is only on one side and therefore on one side of the eye and getting the position right for 2 texts is hard. If I use 3D text and set in front of the players position I think it will go into the wall if a player hits one. Is their any way to display a score on google cardboard / Unity VR.
You can either use native Unity Canvas UI or Googles hack to render OnGUI calls onto a texture.
I would definately recommend Canvas as that is the way Unity is working on their UI features, and it has much better layout capability.
To use canvas, Right click in the hierarchy and add UI->Text. You will automatically get a canvas. The important part is set the canvas to world-space (not screen space overlay). Then drag the canvas game object so it is a child of the Google Cardboard Main head object. Scale it down (like x:0.001,y:0.001,z=0.001) because by default it will be massive. To avoid going through walls position it about 0.5m in front of the camera - within any collider you may have.
there is another approach as well which you can place that canvas under camera make it world space, and then adjust it as you want, after that where ever you look at it will be seen easily ( as suggested by earlier user in answers) as you can see below in picture i placed canvas > Text > under camera which i used for oculus/ google camera

Categories