Good morning everyone, I have a problem with Arrays, I was doing an if where if colorAug=colorBallon then the object was destroyed except that these 2 variables are contained in different Arrays in different scripts in different gameobjects and I tried to get the array, but in the square brackets it asked me for a value that I don’t have because it changes over time, could someone help me? If you need the code to understand or if you don’t understand something tell me.
Share some code so we can better understand the situation.
//ScriptA
Public struct SkinAug
{
Public Color Color
Public string colorAug
}
Public SkinAug[] skinsAug
//ScriptB
Public struct SkinBallon
{
Public Sprite sprite
Public string colorBallon
}
Public SkinBallon[] SkinsBallon
//These are the two Arrays one per script
//If(colorBallon==ColorAug){destroyGameObject} //This is about the code I wanted to write except I have to reference the Arrays and scripts
While this is very pseudo-code, can you give more context on what you are trying to create? Is it some kind of matching game? Just so people understand what might help you the best with what you are trying to do. For example is it more that you could store the gameobjects in a Dictionary with value? That might make it easier to compare things. Or do you actually have gameobjects, each with an array?
Also you might want to use lists. Arrays tend to be when you know exactly how big the array will be, say if a gameobject has an array of always 6 things. I’m thinking that this is some kind of comparison between singular objects, in which case a Dictionary might be useful. Here is an example of using Dictionary and List. Also, a static Dictionary in your class means there is ‘only one’ that is accessible via ScriptA/B.myDictionaryName, rather than storing a dictionary on each. The Update here is only testing the comparison, though you would probably have it as a method to trigger somewhere.
In this case, the Dictionary is storing the GameObject, but also the Component, meaning that if you wanted to edit the names or colours, you could also do this via the Dictionary.
using UnityEngine;
[System.Serializable]
public struct SkinAug
{
public Color color;
public string colorAug;
}
public class SkinAugComponent : MonoBehaviour
{
public SkinAug skin;
public static Dictionary<GameObject, SkinAugComponent> skinsAug = new Dictionary<GameObject, SkinAugComponent>();
void Awake()
{
if (!skinsAug.ContainsKey(gameObject))
{
skinsAug.Add(gameObject, this);
}
}
void OnDestroy()
{
skinsAug.Remove(gameObject);
}
}
using UnityEngine;
[System.Serializable]
public struct SkinBalloon
{
public Sprite sprite;
public string colorBalloon;
}
public class SkinBalloonComponent : MonoBehaviour
{
public SkinBalloon skin;
public static Dictionary<GameObject, SkinBalloonComponent> skinsBalloon = new Dictionary<GameObject, SkinBalloonComponent>();
void Awake()
{
if (!skinsBalloon.ContainsKey(gameObject))
{
skinsBalloon.Add(gameObject, this);
}
}
void OnDestroy()
{
skinsBalloon.Remove(gameObject);
}
}
using UnityEngine;
using System.Collections.Generic;
public class ComparisonScript : MonoBehaviour
{
void Update()
{
List<GameObject> toDestroy = new List<GameObject>();
// Loop through all items in skinsBalloon dictionary
foreach (var balloonPair in SkinBalloonComponent.skinsBalloon)
{
GameObject balloonObj = balloonPair.Key;
SkinBalloonComponent balloonComp = balloonPair.Value;
// Loop through all items in skinsAug dictionary
foreach (var augPair in SkinAugComponent.skinsAug)
{
GameObject augObj = augPair.Key;
SkinAugComponent augComp = augPair.Value;
// Check if the colors match
if (balloonComp.skin.colorBalloon == augComp.skin.colorAug)
{
// Add the objects to the toDestroy list
if (!toDestroy.Contains(balloonObj))
{
toDestroy.Add(balloonObj);
}
if (!toDestroy.Contains(augObj))
{
toDestroy.Add(augObj);
}
}
}
}
// Destroy the objects and remove from dictionaries
foreach (var obj in toDestroy)
{
Destroy(obj);
SkinBalloonComponent.skinsBalloon.Remove(obj);
SkinAugComponent.skinsAug.Remove(obj);
}
}
}
I’m creating a game where the player throws colored needles that can change the color at colored balloons and when the color of the needle matches that of the balloon the needle and the balloon must be destroyed and the score must increase I used the Arrays to make the needle and balloon change color, but if there is a better method I am ready to use it.
I see, well at the point the needle hits the balloon is the only check required, so you wouldn’t need an array or dictionary. The monobehaviour components can just store the values directly. Colliders would detect when there is a collision, and with that, you would check if the 2 items match colour.
GameManager (have just one on a separate “GameManager” gameobject)
using UnityEngine;
public class GameManager : MonoBehaviour
{
public int score = 0;
public void IncreaseScore()
{
score++;
Debug.Log("Score: " + score);
}
// You can add other methods for game management here
}
}
Balloon Component
using UnityEngine;
public class Balloon : MonoBehaviour
{
public Color balloonColor;
// You can add other properties or methods related to the balloon here
}
Needle Component
using UnityEngine;
public class Needle : MonoBehaviour
{
public Color needleColor;
private GameManager gameManager;
void Start()
{
// Link the needle to the gameManager
gameManager = FindObjectOfType<GameManager>();
}
void OnTriggerEnter2D(Collider2D other)
{
Balloon balloon = other.gameObject.GetComponent<Balloon>();
if (balloon != null)
{
if (needleColor == balloon.balloonColor)
{
// Increase the score through the GameManager
gameManager.IncreaseScore();
// Destroy both the needle and the balloon
Destroy(other.gameObject);
Destroy(this.gameObject);
}
else
{
// Destroy only the needle if colors do not match
Destroy(this.gameObject);
}
}
}
}
As you are trying to understand code a bit better, I am keeping it simple here, so hopefully this helps! Based on 2D rather than 3D. Although your next step is to work out how to shoot needles, and make them move towards the balloons.
Also:
-
Add Colliders:
-
Add a Collider2D component (e.g., BoxCollider2D or CircleCollider2D) to both the balloon and needle (prefabs if used).
-
Make sure that the balloon colliders are set to isTrigger.
-
Add a Rigidbody2D:
-
Add a Rigidbody2D component to the needle objects to enable collision detection.
Note that you would probably want to store a list of possible colours in the game manager script, and then access each via a dropdown in the needle and balloons. But I didn’t want to over-complicate the basics here.
Yes, that’s what I wanted to do and had done with the array while I have already done the movement of the needles and balloon