I want to make a sphere popup, just above a cube, when I touch the cube. The sphere will initially be inactive. I just want to activate it when the cube is touched. This is what I have, but it doesn’t work. Any guidance would be greatly appreciated.
void Update () {
if (Input.touchCount > 0) {
if (Input.GetTouch (0).phase == TouchPhase.Ended) {
The first half of what you wrote, the update function, is aimed at the cube. The second half is aimed at the sphere. So this script cannot be attached to the sphere. The first half is checking for input, and therefore the object it is attached to, the cube, must be active. If you were to put the second half on the sphere, it wouldn’t work, because a script on an inactive game object won’t work.
You can do something like this:
public GameObject sphere;
void Update()
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase = TouchPhase.Ended)
{
sphere.SetActive (true);
//sphere.transform.position = new Vector3(transform.position.x, transform.position.y + 5, transform.position.z);
}
}
}
The first line makes a reference to the sphere, so the script knows what object it needs to set active. You would attach this to the cube, and drag in the sphere in the inspector.
The commented out line will make the sphere appear 5 units above the cube in the y direction. So, wherever the cube may be, the sphere will always be 5 units above it when activated. You can adjust the values accordingly in that line accordingly to have it positioned wherever you need. i.e. if you only want it 2 units above, change the 5 to a 2.