Hello,
I only recently started using Unity, and converting my pre-made code to it. Now I am trying to make an object appear at a specific coordinates, in 2D.
I’ll try to explain. The game has main interface window, with some kind of frame. Game rooms need to appear inside the frame. So, in Unity main window, I place one such room manually, to write down coordinates of the game room. Now, I want to write a script, that will place the object in those coordinates.
Just as example, let’s say those coordinates are: (30,40).
To make sure I make myself clear: 30 and 40 are distances between left upper corner of large area and left upper corner of a small area inside.
I tried looking online about how to use those coordinates, and found about Vectors.
But when I am trying to do this:
Vector2 vect1 = new Vector2(30,40);
I get no result. After some trying, I understood, that vector x and y values are supposed to be in a different format, they should be decimals, always between -1 and 1.
How can I convert exact coordinates to this decimal fraction? Or maybe I should use a different approach?
Every GameObject has the Component “Transform” attached to it by default; The Transform component is what holds the data containing its positional data in the game world.
A Vector is a data container containing generic numeric values.
The Transform holds three sets of Vectors: Postition, Rotation (Quaternion if in 3D), and Scale.
You need to first reference the GameObject:
If it is a script which is already attached to the GameObject, then you can simply use Unity’s shortcut by saying transform.position = new Vector2(30, 40);
which is by default a reference to the transform which is attached to the same GameObject that the script is attached to.
or Find your GameObject via tag, name, child index, etc. and say:
gameObject.transform.position = new Vector(30, 40);
By default you should not need to normalize the coordinates between -1 and 1… that sounds like you are following a guide online somewhere and they’ve laid out some degree of limitation based on screen size most likely.
Anyhow, could you post whatever code you are working with?
Just to verify, you are setting them to active again as well?
Perhaps you are not seeing them where you want them because of the line:
thisObject.SetActive(false);
which will disable the GameObjects you have stored in your list.
You can verify this in the inspector if you are unsure, by checking the the box next to the GameObject name after the script has run.
For that matter, is this script actually starting anywhere?
You want to be sure the method StartAllAreas(); is being called in the first place from the Start(), Awake(), OnEnable() or some other place otherwise none of this code will run, including the part in which you set their positions.
(I only ask since you said you are just starting in to re-working a pre-existing script.)
I notice you are using UnityEngine.UI and thus I assume the “GameObjects” are menu elements or something of the sort, and as such, perhaps parented? If so, UI tends to have predetermined behaviors based on their parent objects; Seemingly more so than regular GameObjects.
UI has given me so much pain. I’ll tab the post to check back soon. Hope it works.