How to teleport a gameObject to an empty? (Unity 2021.1)

I am trying to make a respawn system where if the player touches a plane, (the name of the object is tutlvl1_flrdead) they get teleported to an empty object at player spawn named "RESPAWN". But, several tutorials and unity answers threads lead me to nowhere since they were written from back in 2013 or somewhere around there. Please help, I’ve spent days on this!

Code:

using System.Collections; 

using System.Collections.Generic; using UnityEngine;

public class deathfloor : MonoBehaviour { // Start is called before the first frame update void Start() {

}  // Update is called once per frame void Update() {     public gameObject myObject; //You can name it whatever you want.  myObject.transform.position = new Vector3(3.076, 1.74, -3.651); 

} }

You can tell I was using a tutorial lol. Someone please fix my mistakes or help me rewrite this script, thanks!

Gizmos not being drawn until GameObject selected

I have implemented Scene loading in my game and have three scenes: Persistent, Main Menu and Game

By default, the player is loaded into the Persistent scene, which immediately loads the Main Menu, and when the player clicks on Play, Main Menu is unloaded and Game is loaded.

Now, the two active scenes are Persistent and Game.

However, I have a script with OnDrawGizmos on a GameObject in the Game scene. It seems that by default gizmos only begin to be drawn when I click on that GameObject, sort of activating it, and everything works fine. This used to just work before because Game was my only scene and Unity automatically selected that first GameObject. Now, with multiple scenes, no GameObject is selected by default and my gizmos won’t draw until I manually click on it.

How can I solve this?

How to Make GameObject move in Circular Motion

I’ve been trying to work an enemy that moves in a circular motion for my RPG game, but for some reason, whenever I press play the GameObject instantly goes hundreds of units in the X and Y coordinates. It also move back to -1 on the Z axis. Here’s the script to my enemy’s movement:

using System.Collections; using System.Collections.Generic; using UnityEngine;  public class EnemyScript : MonoBehaviour {      private Rigidbody2D rb;      [SerializeField]     float rotationRadius = 2f, angularSpeed = 2f;     float posX, posY, angle = 0f;      // Start is called before the first frame update     void Start()     {         // Gets the RigidBody2D component         rb = GetComponent<Rigidbody2D>();     }      // Update is called once per frame     void Update()     {         Movement_1();     }      void Movement_1()     {         posX = rb.position.x + Mathf.Cos(angle) + rotationRadius;         posY = rb.position.y + Mathf.Sin(angle) + rotationRadius;         transform.position = new Vector2(posX, posY);         angle = angle + Time.deltaTime * angularSpeed;          if (angle >= 360f)         {             angle = 0f;         }     } } 

Change Text – Inside A button, inside a Canvas, inside a GameObject: Access all the texts inside of hierchy?

 //.... Gameobject tile; int count = 1; float posX = 0; float posY = 0; ....//  GameObject blueTile = (GameObject)Instantiate(Resources.Load("block_blue")); tile = (GameObject)Instantiate(blueTile, transform);  for loop {    ...    ...    tile.GetComponent().GetComponentInChildren().text = count.ToString();    tile.transform.position = new Vector2((float)posX, (float)posY);    count++; }  

enter image description here enter image description here

As you can see by the image. My line of thinking goes like this; I used to create Flash/AS3 Applications and Games. I have used a number of API’s and they usually have a lot in common. Unix, however, is very different and I may have not grasped the key concepts.

I want to access my PreFab, named in Assets/Resources (the root path)
“block_blue”

All I wan to do is set up a row and columns of a box with a number inside of it.

Before you go and ask, why the complicated mix of Objects? Well, Adobe had MovieClip and use of layers and also use of a timeline. Unix doesn’t seem to have this in setting up a Scene (the Stage in Flash).

So, I set up a block that looks like this (right of main image 66): It’s a Canvas, to hold a Button, the button has a text. To move the Canvas, I put the Canvas inside a GameObject. This, to me, is like a MovieClip in Flash API.

**GameObject > ButtonLight, Button > TextBlack, Text**

I need to access TextBlack and also Text. Pretty easy right? I’ve been at this for 4 days, many many hours.

EDIT: It seems that it is working, somewhat.
Looking at the reference photo and the Hierarchy reference, it looks like the blackText behind the blueText is changing as expected. But I cannot access the top BlueText. The Blue Text is also a child of type Text, but it’s a second (layer?) copy of the text inside of the Button. So, it’s getting the first Child Text, but how to access the second Text?

Unity TextMeshPro bounds.size is wrong when hovering first time over gameobject

I have this weird issue, which I am not able to fix. This is my third attempt and I need some help. Basically I have a hover element which spawns when I hover over a gameobject. The height of the element is determined by the amount of text (TextMeshProUGUI).

The issue happens only when I hover first time, also not always (I have no clue how to reliably to reproduce it).

Here is what happens (sec 10~):

I have tried to add these everywhere and different combinations:

LayoutRebuilder.ForceRebuildLayoutImmediate(hoverElementRect); hoverElementRect.ForceUpdateRectTransforms(); Canvas.ForceUpdateCanvases(); txtMeshPro.ForceMeshUpdate(); 

They dont seem to help. I assume the problem is here:

float bgHeight = txtMeshPro.bounds.size.y + 25; 

The bounds.size.y is wrong the first time I hover, but I have no clue why.

Here is the whole code for spawning the GUI element:

public static void SpawnGeneralHoverElement(string descText, RectTransform hoverOverRect, float guiWidth) {     Canvas hoverCanvas = instance.hoverElementGeneral.GetComponent<Canvas>();     EnableCanvas(hoverCanvas);      TextMeshProUGUI txtMeshPro = hoverCanvas.GetComponentInChildren<TextMeshProUGUI>();     RectTransform hoverElementRect = hoverCanvas.GetComponentsInChildren<RectTransform>()[1];      float scale = hoverCanvas.GetComponent<RectTransform>().localScale.x;      txtMeshPro.text = descText;      // Need this to be dynamic, if I change hover element size, it will still work     float hoverElementWidth = guiWidth;      // Forces canvas update because of: stackoverflow.com/questions/55417905/why-is-textmeshpro-not-updating-textbounds-after-setting-text     Canvas.ForceUpdateCanvases();     LayoutRebuilder.ForceRebuildLayoutImmediate(hoverElementRect);     hoverElementRect.ForceUpdateRectTransforms();     txtMeshPro.ForceMeshUpdate();      // Need this because otherwise the element spawns too far away on smaller resultions     float spawnOffset = (txtMeshPro.bounds.size.y * hoverCanvas.GetComponent<RectTransform>().localScale.x) + (Screen.height * 0.022f);      float bgHeight = txtMeshPro.bounds.size.y + 25;      hoverElementRect.position = new Vector3(hoverOverRect.position.x, hoverOverRect.position.y + spawnOffset,         hoverOverRect.position.z);      hoverElementRect.sizeDelta = new Vector2(guiWidth, bgHeight);     Debug.Log(hoverElementRect.sizeDelta);      LayoutRebuilder.ForceRebuildLayoutImmediate(hoverElementRect);     hoverElementRect.ForceUpdateRectTransforms();     Canvas.ForceUpdateCanvases();     txtMeshPro.ForceMeshUpdate();      // Slide in form left to right if off bounds, need for traps for example     float leftOffset = hoverElementRect.position.x - ((hoverElementRect.sizeDelta.x * scale) / 2);     if (leftOffset < 0) {         hoverElementRect.position = new Vector2(hoverElementRect.position.x + (leftOffset * -1) + 15, hoverElementRect.position.y);     }      // Slide in form right to left if off bounds, need for traps for example     float rightOffset = Screen.width - (hoverElementRect.position.x + ((hoverElementRect.sizeDelta.x * scale) / 2));     //Debug.Log("Screen width: " + Screen.width + "\nrightOffset: " + rightOffset);     if (rightOffset < 0) {         hoverElementRect.position = new Vector2(hoverElementRect.position.x - (rightOffset * -1) - 15, hoverElementRect.position.y);     }      LayoutRebuilder.ForceRebuildLayoutImmediate(hoverElementRect);     hoverElementRect.ForceUpdateRectTransforms();     Canvas.ForceUpdateCanvases();     txtMeshPro.ForceMeshUpdate();  } 

SetActive doesn’t work for GameObject

I’m using Unity 2D for my game and I’m facing a weird problem. I have a GameObject (an image) in my scene with a button attached to it. When the player is out of the screen, the image that works as a GameOver panel appears and in case of enough coins, you can resume the game. Everything works fine but when the image appears and I click on button to resume, the image doesn’t disappear while the function for decreasing number of coins still works.

Here is my script in which di() is for die and but() for button:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using CodeStage.AntiCheat.ObscuredTypes; using CodeStage.AntiCheat.Detectors;  public class UIManager2 : MonoBehaviour {      public static ObscuredInt coin_score = 0;      public Text coin_text;     public Slider slider;     public Slider slider1;     public Slider slider2;     public GameObject bul;     public bool reverse;     public float timer;     public int delay = 1;     public float speed=0.5f;     public GameObject pause;     public AudioSource[] aud=new AudioSource[3];      void Start () {         coin_score = ObscuredPrefs.GetInt ("Score");         StartCoroutine (elapsed ());         slider1.minValue = 0;         slider1.maxValue = 20;         bul = GameObject.FindGameObjectWithTag ("Player").GetComponent<Plane19> ().bullet;     }      void Update () {         timer += Time.deltaTime;         if (timer >= delay && reverse == false) {             timer = 0f;             slider2.value++;         }          if (timer >= delay && reverse == true) {             timer = 0f;             slider2.value -= speed;       }          coin_text.text = coin_score.ToString ();         ObscuredPrefs.SetInt ("Score", coin_score);          if (slider2.value == 10) {             bul.SetActive (false);             reverse = true;         }       if (slider2.value == 0) {             bul.SetActive (true);             reverse = false;         }      }      public void di(){         pause.SetActive(true);         GetComponent<AudioSource>().Pause();         Time.timeScale = 0;         aud[0].Play();         aud[1].Pause();         aud[2].Pause();  }      public void but(){         pause.SetActive(false);         Time.timeScale=1;         aud[0].Pause();         aud[1].UnPause();         aud[2].UnPause();         GetComponent<AudioSource>().UnPause();         UIManager2.coin_score-=2;      }  IEnumerator elapsed () {         yield return new WaitForSeconds (2f);         slider.value++;         StartCoroutine (elapsed ());     } } 

How to animate two or more unrelated properties on a sprite GameObject using animation layers?

I have a sprite in Unity 2019.3. It is animated like so (with provided mockups):

  • Its base animation is just one frame; i.e. it has the same Sprite whether idle or moving.
  • When the player is invincible, the ship flashes in and out. This is implemented by setting the alpha to 0 and 1 and back.
  • The player can acquire up to three levels of shields. The is implemented as a child GameObject with its own Sprite (but not its own Animator).
  • When a player acquires a power-up, they briefly flash colors. This can happen while they’re shielded, invincible, or both. This is implemented with a custom palette-swap shader, not by replacing the Sprite.

These animations are all independent of one another, and can all occur at any time. For example, when a shielded player takes a hit their shield downgrades but they also gain brief invincibility. It would look like this:

A single state machine to account for these possibilities (and others, depending on whether or not I add other power-ups or mechanics) would be prohibitively complex. So I’m going with animation layers.

I was hoping that it would be as simple as adding separate animation layers with their own state machines, then marking each layer as additive. Nope. I have no idea what I’m doing.

How can I use Mecanim to animate my sprite as I have previously described?

Calculate orientation of GameObject in custom coordinate system in Unity

I am trying to build a custom coodinate system. To be more specific, the user chooses three points on screen to define the coordinate system. The first one is the origin, the second one is used for building the OX axis, the third one OZ and the last one OY. The system is corectly built. The axis are perpendicular to one another.

After this coordinate system is built, I need to convert the orientation and position of objects placed on screen from world space to this particular world space. In order to do that, I have tried the proposed solution from here 1. It seems to be working, but only for the position. The orientation seems to not change at all.

For instance, if I place a cube at (0, 0, 0) without rotating it in world space, in my own coordinate system it should also be at (0, 0, 0) and it should not be rotated relative to to my own coordinate system. So, the cube in my coordinate system should look rotated regarding the world space. However, in reality, my code places the cube at my own origin, but does not rotate it accordingly.

In the image below, the first coordinate system (the smaller one) is my own system. The other one is the world origin. It can be seen that the WorldObject cube is placed at (0, 0, 0) with rotation (0, 0, 0). The OwnObject should be the representation of WorldObject in my system. It can be seen that the position has changed, but not the rotation.

My question is how can I rotate the second cube accordingly? I have tried to set the rotation of the cube to be the orientation from the structure proposed in this solution 1, but it did not seem to rotate correctly. I have also tried to use Quaternion.LookRotation, but it did not work either.

enter image description here 1 How to create a custom set of coordinates based on two points in Unity?