Hi , i am making a 2D catch game . And i want to create a warning were the item will land before it will shown on the screen so the player have more time to move to the right position.
I already created a sprite. But i dont know how to script it.
1.Object will spawn on y = 7.5 and on the same X as the object the player must catch
2. It should disappear as soon the object will be catched or destroyed by ground.
3. Its an animation ( 3 sprites )
I dont know how to access the x Cords from the flying object (this one which player catch ) and destroy the warning
I didn’t look at your code because I don’t want to download your files. However, to obtain the coords of a GameObject there’s a lot of ways to approach it.
public Transform flyingObjectTrans;
// This will store the position of your object
private Vector2 flyingObjectCoords;
// This variable will keep track of your specific axis
// When you want to obtain the X-axis or Y-axis of the object
void Update()
{
flyingObjectCoords = new Vector2(flyingObjectTrans.position.x, flyingObjectTrans.position.y);
}
yes , but my items spawns in a ienumarator and i cant access it (i dont know how )
thx for help
Game Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
[SerializeField] private GameObject itemPrefab;
[SerializeField] private float Position;
float item;float warning;
public Text ItemText;
public int Itemvalue;
public void Start()
{
StartCoroutine(ItemSpawnen());
Itemvalue = 45;
UpdateItem();
}
public IEnumerator ItemSpawnen()
{
for (int i=0; i<=44; i++)
{
GameObject item = Instantiate(itemPrefab);
float xPosition = Random.Range(-7.0f, 8.0f);
Vector2 position = new Vector2(xPosition, 10);
item.transform.position = position;
float Spawntime = Random.Range(0.9f, 2f);
yield return new WaitForSeconds(Spawntime);
Itemvalue = Itemvalue - 1;
UpdateItem();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PickUp : MonoBehaviour
{
public Text scoreText;
public int scorevalue;
void Start()
{
scorevalue = 0;
}
// Update is called once per frame
void Update()
{
Well looks like you’re making your item a local variable. You could create a global variable and return the information within the IENumerator which may require you passing a variable in. You could also declare your item variable as a global and then initialize it in the same spot it already exist. That way you can access it throughout your script. You can also declare another variable of Transform type within the IEnumerator and obtain its Transform.position information that way.
public IEnumerator ItemSpawnen()
{
for (int i=0; i<=44; i++)
{
GameObject item = Instantiate(itemPrefab);
// Here you will make a transform variable to keep track of position
Transform itemPosition = item.transform.position;
// Or you could store the information inside of a Vector2
// As I demonstrated earlier
// *** Alternative Below***
Vector2 itemPositionVector = new Vector2(item.transform.position.x, item.transform.position.y);
float xPosition = Random.Range(-7.0f, 8.0f);
Vector2 position = new Vector2(xPosition, 10);
item.transform.position = position;
float Spawntime = Random.Range(0.9f, 2f);
yield return new WaitForSeconds(Spawntime);
Itemvalue = Itemvalue - 1;
UpdateItem();
}
If you need more help with this, feel free to ask
Also, use the Code tags as demonstrated here: (Makes code more readable)
Your Top script here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameController : MonoBehaviour
{
[SerializeField] private GameObject itemPrefab;
[SerializeField] private float Position;
float item;float warning;
public Text ItemText;
public int Itemvalue;
public void Start()
{
StartCoroutine(ItemSpawnen());
Itemvalue = 45;
UpdateItem();
}
public IEnumerator ItemSpawnen()
{
for (int i=0; i<=44; i++)
{
GameObject item = Instantiate(itemPrefab);
float xPosition = Random.Range(-7.0f, 8.0f);
Vector2 position = new Vector2(xPosition, 10);
item.transform.position = position;
float Spawntime = Random.Range(0.9f, 2f);
yield return new WaitForSeconds(Spawntime);
Itemvalue = Itemvalue - 1;
UpdateItem();
}
}
void UpdateItem()
{
ItemText.text = " " + Itemvalue;
}
}
And your bottom script here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PickUp : MonoBehaviour
{
public Text scoreText;
public int scorevalue;
void Start()
{
scorevalue = 0;
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (!collision.gameObject.tag.Equals("Respawn"))
{
return;
}
Destroy(collision.gameObject);
scorevalue = scorevalue + 1;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = " " + scorevalue;
}
}
To find the button that produces this magic, look on the ribbon of the chat box:
You should do the community a solid and post your completed code so if anyone has a similar issue, they can find an answer. That’s up to you though. I think it’s a nice thing to do though.