- Home /
Instantiated GuiTextures not showing
I have a js script that spawns objects in a random x position in the sky, and spawns a GUITexture in the same location, but at ground level. When the player is facing the guitexture (It's a 2d game with 3d graphics, the camera cannot rotate) it doesn't show. However, when I manually drag the guitexture into view of the camera, it is clearly there. If you'd like to see any scripts, I will, but I don't see how the script is causing the problem.
Answer by Eric5h5 · Mar 30, 2014 at 04:18 PM
GUITextures use viewport coordinates, which must be between 0 and 1. (For X and Y anyway; Z is used for sorting and can be anything.)
How would I make my script take this fact into account?
using UnityEngine;
using System.Collections;
public class CoinSpawner : $$anonymous$$onoBehaviour {
public bool spawn;
public Vector3 pos;
public Vector3 symbolpos;
public float time;
void Start()
{
spawn = true;
}
void Update()
{
time = time+0.1f;
if(time >= 30&&spawn == true)
{
time = 0;
pos = new Vector3(Random.Range(-45.0f, 45.0f), 50, 0);
symbolpos = new Vector3(pos.x, 0.9255f, 0);
GameObject go = Instantiate((Resources.Load("coin")), pos, Quaternion.identity) as GameObject;
GameObject coinSymbol = Instantiate((Resources.Load("coinSymbol")),symbolpos, Quaternion.identity) as GameObject;
Destroy (go, 5.0f);
Destroy (coinSymbol, 3.0f);
}
}
}
Probably convert world space to viewport space, using WorldToViewportPoint.
I've updated my code, but I still can't seem to get it working
using UnityEngine;
using System.Collections;
public class CoinSpawner : $$anonymous$$onoBehaviour {
public bool spawn;
public Vector3 pos;
public Vector3 symbolpos;
public float time;
public Vector3 viewPos;
private Vector3 screenPos;
void Start()
{
spawn = true;
}
void Update()
{
time = time+0.1f;
if(time >= 30&&spawn == true)
{
time = 0;
pos = new Vector3(Random.Range(-45.0f, 45.0f), 50, 0);
symbolpos = new Vector3(pos.x, 0.9255f, 0);
screenPos = Camera.main.WorldToScreenPoint(symbolpos);
screenPos.y = Screen.height - screenPos.y;
GameObject go = Instantiate((Resources.Load("coin")), pos, Quaternion.identity) as GameObject;
GameObject coinSymbol = Instantiate((Resources.Load("coinSymbol")),symbolpos, Quaternion.identity) as GameObject;
Destroy (go, 5.0f);
Destroy (coinSymbol, 3.0f);
}
}
}