Unity GameObject rendering below GameObject lower in the hierarchy - unity3d

I have a GameObject (TopSquare) that is rendered after PlayButton in the hierarchy, yet the PlayButton is showing on top. This goes against Unity's rule of objects added last are shown over objects added previously.
Why is this?

The square is a sprite that is an object in the World, while the button exists in the UI layer over it.
You have to use UI Image for black square instead of sprite renderer (Right-click on parent -> UI -> Image)

Related

Unity3d how to have a "text label" follow a game object

In my world I have a GameObject, lets say a race car.
Above the race car I want a white box with a text label that says the driver's name (let's say "Fred"). This label will follow the race car as it moves.
How can I create this "white box with a text label" in Unity? Will it be a gameobject (if so, how do I construct it).
Since the only "Text" object I find in Unity editor was a UI element. And the UI canvas is unrelated to the coordinates in my game world, so I dont know if this is appropriate use case for this type of object.
I think you have 2 options. It is fundamental to read to the different canvas modes in the documentation
1.- Overlay mode. You can figure out the screen position of your canvas at runtime with
Camera.WorldToScreenPoint. This canvas mode places UI elements on the screen rendered on top of the scene, so you would kind of "fake" the canvas is following your gameObject in the world calculatig and updating its position.
2.- World Space, make cavas children of your car and make it always face the camera with a billboard component updating the canvas rotation.
public class Billboard : MonoBehaviour {
public Transform cam;
private void Start() {
cam = Camera.main.transform;
}
void LateUpdate() {
transform.LookAt(transform.position + cam.forward);
}
}
Add the component to the canvas gameObject or do myCanvas.AddComponent<Billboard>(); where myCanvas is the Canvas component in your canvas holder gameObject.
With this option the canvas is actually in the world as the mode name indicates, thats why you would need to update its rotation to make it always look to the camera.
In option 1 you need to update the position and not the rotation because the canvas is in the screen and with option 2 you need to update the rotation and not the position because the canvas is in the world.

In unity why cannot Instatiate a gameobject

I am currently coding in Unity and I am trying to Instantiate a gameObject. The mechanism I'm developing will delete the Image I'm on and load the image underneath, using two buttons ; One to create and one to delete thus giving the effect of moving through a graphical stack.
I have thought about moving the image forward along the z axis so it is out of view, but I have read that the object MUST be instatiated and not Transformed
I have a Rigidbody2d and (2x) 3D Box Collider over the image that has the code. One on the left and one on the right
The delete button works a treat and just to check I do not have a problem with the box collider I have tried the delete code on the other button which also works fine, so I have come to the conclusion that the code is flawed in some respect
My problem is that I cannot seem to recreate the object once it has been deleted, it is my understanding that the object does not have to exist in world space for it to be recreated. If I use the Quaternion Identity I get a few errors about the gameObject being rendered off screen
***
Instantiate(gameObject,new Vector3 (8,-17,11),
// Removed until I can actually get the image to Instantiate
Quaternion.identity);
***
I have also tried
***
Instantiate(gameObject,new Vector (gameObject.transform.position.x -0.08, gameObject.transform.position.y, -0.17, gameObject.transform.position.z, 11), Quaternion.identity);
}
}
***
Can someone please tell me what I am doing wrong?
I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"
I am fairly new to Unity too, but I believe what you need is a Button that Swaps the Sprite image.
Select your Button in the Hierarchy.
In the Inspector:
In the Image component, drag in the first Sprite
In the Button Component:
select the check box Interactable
select Sprite Swap in the Transition Drop Down
drag your second Sprite to "Selected Sprite"

Mask on ScrollRect doesn't hide child mesh objects outside of the slider (but works for text)

I have a slider scrollrect object which I populate at run time with a bunch of space ship equipment and weapons which are prefabs. The prefabs have text objects (e.g. descriptions of the equipment) as well as mesh objects. The problem I'm having is that when the player scrolls through the prefabs and they slide outside the scrollrect perimeter the text is hidden (as it should be), but the meshes are NOT hidden!
Here's the scene setup:
https://i.stack.imgur.com/sR97f.png
Any ideas what I'm doing wrong?
You can use a RenderTexture with a RawImage component and camera pointed on each inventory item. See https://answers.unity.com/questions/844742/render-texture-in-ui.html
Otherwise prerender your items to thumbnail images.

How can i make the second camera to display in the bottom right corner in a small window only specific gameobject?

I added a new layer called it: CameraLayer.
I have one Camera as child under the first ThirdPersonController tagged as MainCamera. I set that this camera will show everything but the CameraLayer. In the inspector in Culling Mask. Everything marked but not the CameraLayer. And now i see in the Culling Mask: Mixed. This is the main camera showing everything.
The second camera i added as child under a Cube. And i want that this camera will show only the cube all the time in a small window in the bottom right corner in the Game View window while the game is running.
In this camera Culling Mask i selected only the CameraLayer and unselected the rest. So in this camera in the Culling Mask i see: CameraLayer.
But when i'm running the game i don't see the Camera under the Cube at all.
In the screenshot i marked in black where should the Camera under the Cube should be and display the Cube:
1) Create a Canvas as child of your first Camera then create a RawImage as a child of this newly created Canvas.
This is this Image that will render your second camera video so move it at the bottom right of the view of your first camera.
2) Then Create a RenderTexture and assign it to the Texture property of the RawImage you just created
3) And finally assign this RenderTexture to the Target Texture property of your second Camera.
This Target Texture will be used by Unity as a buffer Texture for your Camera. Your scene should now look like this:

Sprite not displaying

I'm facing a problem in my scene i drop a object ball in hierarchy and arrange its position in center. But it's not showing that ball in Scene and Game tab it only shows its circle collider radius like this:
But when I untick and then tick again back to a component sprite renderer then it's show that object in Scene and Game tab like this:
Check your material. Possibly this material have selected not-existed shader.
Check Sorting layer. It must be higher than background Sorting layer