Instantiated 2D text doesn't show up in Unity - c#

Excuse me for any grammatical errors. (I know that there are many questions like this here, but I haven't found a solution yet).
I'm trying to instantiate a 2d object with a 2d text, the problem here is that the text is invisible when instantiated. Yes, I know that I have to set a canvas as parent of it but it is still not working...
Code:
Instantiate(levelAsteroid, new Vector3(-7, 2.25f, 0), Quaternion.identity,
GameObject.FindGameObjectWithTag ("Canvas").transform);
Unity hierarchy, when the object is instantiated:
Canvas settings:
Update:
I think I found the problem.
If I just instantiate the object, it works fine, the text is visible, but if I try to change the text from the script, the text becomes corrupt, after that the text won't never show up not even if I put the object in the canvas manually.
FIXED:
The problem seems to be the way I used to change the text..
Before, I used to instantiate the object and change the text from a script attached to the GameController, now I change the text from a script attached to the object that has the text as child.
Before:
(Script attached to the GameController):
public GameObject exampleOfObj;
void instantiateObj(){
object = Instantiate(exampleOfObj, new Vector3(-800, 300f, 0), Quaternion.identity);
object.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, false);
objectText.SetText ("Text: " + value);
}
I simply removed the call to the "SetText" method and I have put this into the script that is attached to the object that I'm instantiating.
I found a solution but I don't understand why it was a problem.

It's hard to figure out your issue without additional information. Possible solution is use Transform.SetParent method with worldPositionStays parameter set to false instead of instantiating on parent transform. You can find troubleshooting of similar issues in Unity Documentation. See Instantiating the UI element section.

I did exactly as you did and it is showing the text. My canvas settings are :
Render Mode : Scale Space - Camera (Camera Attached)
UI Scale Mode : Scale With Screen Size
Reference Resolution : 1920 x 1440
Match = 0.5
Also make sure when you put the object in the canvas manually, it is also showing the text.

Related

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!

Material property change via code doesn't show

I'm using Unity 2020.3.3f1 (HDRP)
I have a prefab (cube) which has a emissive material on it.
After pressing the mouse button, I want it to increase its Emission intensity by 10.
Problem that I encountered:
The Inspector shows me that the Intensity is infact changing but the Game does not represent these changes (this means it's not getting "brighter" even though the material property says it does).
Now when I increase the amount via the Inspector manually, just by 0.1 even, all of the sudden the changes are now visible.
I think I tried everything now without luck...
How the code looks like in a nutshell:
public Material cubeMaterial;
private float intensity = 10;
if("mouseClick"){
intensity += 100;
cubeMaterial.setFloat("_EmissiveIntensity", intensity);
}
I suppose you are using the default HDRP/Lit shader for your material. If that's so, you can access your cube emission intensity via "_EmissiveColor" shader keyword like this:
cubeMaterial.GetColor("_EmissiveColor");
which returns a Color value.
And you can modify it in a similar way:
cubeMaterial.SetColor("_EmissiveColor", startingEmission * 1.1f);
In general, when working with HDRP material shaders it's always safe to look up the shader keywords, you can do it via navigating to your material in the inspector, clicking the kogwheel and selecting edit shader, which opens the .shader file.
If you intend to modify only this gameObject's material, I recommand you to use MaterialPropertyBlock to modify a property of your material.
If you don't, a new material will be created behind the scene and can lead to memory problems.
To do this, get a reference to the gameObject's renderer, get its property block, modify it and reassign the modified property block.
You can learn more on this documentation
Hope that helped ;)

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

Sprites drawing over text in unity

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

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.

Categories