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!
Related
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.
I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
You could do it by using OnMouseDown() function. Here is the link Unity Docs
You will also need to add the image script along with the button script.
I have a 2D polygon that is defined by a edgeCollider. Is there a way to hide everything that is outside of the shape and show only what is inside?
I tried using skyboxes and lights. I thought about creating a mask(but i dont know how to create such a mask).
Is there a way to only show what is inside the shape defined by edge collider?
What about using the colliders trigger events?
Set this polygon as a trigger in the edgeCollider2D component in the inspector. Then you can use the collider OnEnterTrigger2D.
Your gameobjects are all disabled until this edgeCollider collides with in. Then disable the gameobject OnExitTrigger2D.
If you wanted to limit it to a certain number of object only. You would set a layer to only hide/show these object.
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.layer == "hiddenObject"){
other.gameObject.enable = true;
}
}
Then the reverse on the OnTriggerExit2D.
I'm not sure the effect that you are aiming for. So another solution could be a postprocessing shader.
I can only give a high level description on this however.
You would take the final screen image texture, and the current position of the polygon and then add the pixels from the screen texture to the polygon texture and output this texture. (this shader has to exists already).
But you want the inverse of these.
https://docs.unity3d.com/Manual/shader-TransparentCutoutFamily.html
You're wanting to keep what's in the hole and get rid of what's outside of it?
You can download the built in shaders to edit them here
https://unity3d.com/get-unity/download/archive
Just find your version of unity and select builtin shaders from the drop down.
Edit: "SPOTLIGHT!", try this but with your custom shape
http://www.shaderslab.com/demo-49---spotlight.html
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.
I'm working on a version of Conway's Game of Life in Unity, and here is my setup for making the grid:
I've created a prefab of an individual cell responding to mouse click that will be the basis for creating the cell grid. I have a Empty GameObject to act as the controller to create the grid. I'm putting it in the code for the controller like so, pointing my prefab to the field:
[SerializeField]
private GameObject Cell;
private Camera _camera;
My idea was to get the dimensions of the Cell and instantiate it into a grid, with _camera pointing to the Main camera to get boundaries. However, I'm not sure how to get the height/width from GameObject. What's the best way to find this out?
I don't know if you found the answer, but the most common way is using Collider (if you have one, but mouse click needs it) or Renderer (if you have a mesh) by using:
GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size
Game of life is very nice, I've written my paper for bachelor seminar about it. Have fun!