Ok so im trying to create some floating text for my 2d clicker game and followed a fairly simple tutorial for it. However when my gameobjects spawn they do not spawn at the location of the prefab. here is the code for it.
Note that the gameobjects are also not being parented to “Enemy” as they should…
Any help is appreciated thanks.
There are 2 problems.
First, as @GioVil points out, transform.FindChild(“Enemy”) will look for an object called ‘Enemy’ that is a child of whatever object your script is running on. Presumably this isn’t what you want - I’d guess (as @GioVil says) from your scene that you want to search the scene for an object called "Enemy’, and use it’s transform:
However that obviously doesn’t work well once you have multiple enemies in the scene. If that’s the case then you’ll need to find the enemy you’re interested in somehow, though we obviously can’t help in that department without knowing a little more info.
For the position, this line:
tempRect.transform.localPosition = CBTprefab.transform.position
is setting the local position (i.e. the position relative to its parent) of tempRect to the world position (i.e. the position in the world the prefab was saved with) of the prefab. If what you want is for the tempRect to appear at the same position as the prefab was when you saved it, you would need to just set its position:
tempRect.transform.position = CBTprefab.transform.position
Assuming that’s the behaviour you’re after, it should solve your problem.
FYI: in future, post code as text using the code formatting (see the 1s and 0s icon in the text box), not with screen shots - it makes it hard to copy and paste when writing replies!
Because the method “transform.FindChild” works to find a child of a transform where the script is attached.
You can use GameObject.Find(“Enemy”).transform