Good morning everyone, I don’t know how to do something. I have this game object with the currentIndex variable which is generated randomly , I would like to store the index of the gameobject colliding with another gameobject in the collisionIndex var, because now it shows me the currentIndex value of the last cloned gameobject and not of the collided one, but I don’t know how to record the collided one, not I know if there is a code that allows me to do this.Tell me if you need the code or if you don’t understand something. Thank you
Your information is incomplete, and a little bit incoherent. Try to offer some code examples (with code tags) and/or screenshots of your scene/hierarchy/inspector, so we can gather a better mental image of what you’re doing.
using TMPro;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;
public class BallonController : MonoBehaviour
{
public float speed = 0.3f; // Velocità di movimento verso il basso
public float horizontalRange = 1.0f; // Gamma del movimento orizzontale
public float horizontalSpeed = 1.0f; // Velocità del movimento orizzontale
public float changeDirectionInterval = 1.0f; // Intervallo di tempo per cambiare direzione
private float timeSpawner;
private float maxTime = 1.5f;
private Rigidbody2D rb2d;
private float horizontalDirection;
private float nextChangeTime;
public GameObject enemyPrefab;
//Script
public ChangeColor changeColor;
public GameController gameController;
//Struttura per memorizzare il colore e la sprite
[System.Serializable]
public struct SkinBallon
{
public string colorballon;
public Sprite sprite;
}
// Array di skin
public SkinBallon[] skinsBallon;
public int currentIndex;
// Flag per determinare se questo è l'oggetto originale
private bool isOriginal = true;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
horizontalDirection = Random.Range(-0.5f, 0.5f);
nextChangeTime = Time.time + changeDirectionInterval;
timeSpawner = maxTime; // inizializza il timer di spawn
// Ottieni il componente SpriteRenderer del nemico
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
// Imposta un colore casuale
currentIndex = Random.Range(0, skinsBallon.Length);
spriteRenderer.sprite = skinsBallon[currentIndex].sprite;
Debug.Log("Palloncino creato con indice: " + currentIndex);
}
void FixedUpdate()
{
if (!isOriginal)
{
Vector2 moveDirection = Vector2.down * speed;
moveDirection += Vector2.right * horizontalDirection * horizontalSpeed;
rb2d.velocity = moveDirection;
if (Time.time > nextChangeTime)
{
horizontalDirection = Random.Range(-0.7f, 0.7f);
nextChangeTime = Time.time + changeDirectionInterval;
}
if (transform.position.x < -horizontalRange || transform.position.x > horizontalRange)
{
horizontalDirection = -horizontalDirection;
}
}
if (isOriginal)
{
timeSpawner += Time.deltaTime;
if (timeSpawner >= maxTime)
{
GameObject enemyObject = Instantiate(enemyPrefab, new Vector3(0f, 3.0f, 0f), Quaternion.identity);
BallonController cloneController = enemyObject.GetComponent<BallonController>();
cloneController.isOriginal = false;
timeSpawner = 0f;
}
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (!isOriginal && collision.gameObject.CompareTag("Player"))
{
// Verifica che il palloncino corrente stia usando il giusto indice
Debug.Log("Palloncino in collisione con indice: " + currentIndex);
// Controlla il colore dell'ago e del palloncino in collisione
string colorAug = changeColor.SkinsAug[changeColor.currentIndex].colorAug;
string colorBallon = skinsBallon[currentIndex].colorballon;
Debug.Log("Colore dell'ago: " + colorAug);
Debug.Log("Colore del palloncino: " + colorBallon);
if (colorAug == colorBallon)
{
gameController.IncreaseScore();
Debug.Log("Punteggio: " + gameController.score);
}
Destroy(gameObject);
}
else if (!isOriginal && collision.gameObject.CompareTag("Floor"))
{
Destroy(gameObject);
}
}
}
This is the code of the gameobject
I wanted to put an int to record the currentIndex of the balloon that is colliding with another object
I am assuming you want the Player object to be able to get the currentIndex of the last Ballon it collided with. If this is correct, then you can accomplish this by having the Player look for the BallonController on the collided object. If this exists, then the Player can access the currentIndex public variable.
It may help to add a “Balloon” tag to all of the BallonController objects so that the Player doesn’t need to do a GetComponent search on every collision.
Something like this could be done in the Player script (or whatever script is trying to get the currentIndex)
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Balloon"))
{
BallonController ballon = collision.GetComponent<BallonController>();
if(ballon != null)
{
Debug.Log("Ballon index is " + ballon.currentIndex)
}
}
}
Thanks this was what I wanted to do, now I’ll try to see if it works
your code doesn’t work, but the idea is right.
Ok I resolve the problem.Thank you all