I don't understand Prefabs

Hi there. I’m trying to learn Unity, and I’ve followed a video online doing balloons that go up vertically and that you click to explode. So far nothing hard.

The issue happens when I put my “Balloon” GameObject in my “Prefabs” Folder. The Text and Life Text goes missing, and if I try to add it, it’s a Missmatch. Why ?

When I drag the Prefab back to the Hierarchy, I can modify it correctly. I just can’t while it’s in the prefabs. And I’d think the whole point of the prefabs would be not to have to modify manually each copied one ?

Also, at first the scores were individual for each prefab. I changed my script to have it be a “static”. It works, but is it how you should do it ?

I hope I’m clear enough,

Thanks a bunch

Objects in your projects assets cannot reference objects in scenes, though the reverse is allowed. Those references get cleared out as that type of reference isn’t possible.

Ideally a separate component should be handling life and score, rather than the balloon itself. Another can be spawning them, and listening for them being destroyed.

1 Like

Mhm… I’m trying to understand how it would work.

So I cannot have stuff in my prefab that reference objects in scenes. So my balloons prefab would only be about their script, moving up the screen, being reset and popped, and that’s it ? Something else should manage my health and the overall score ? Like, another script ?
I’m really not the best at this :frowning:

Pretty much! You instantiate the prefab at the desired position and let it do its thing. At the same time as instantiating it, you can pass it some references, or hook into it potentially. It would be a good case to use a C# delegate to listen for when the balloons are destroyed.

Indeed, another script, aka another custom monobehaviour component. Helps to break up responsibility into different components. In this case because you want to have something persistent in the scene that can update the UI, and keep track of score and lives.

Here’s some code hashed together to give you an idea:

public class Balloon : MonoBehaviour
{
	[SerializeField]
	private float _movementSpeed;
	
	private Rigidbody2D _rigidbody;
	
	public event System.Action OnBalloonDestroyed;
	
	private void Awake()
	{
		_rigidbody = GetComponent<Rigidbody2D>();
	}
	
	private void FixedUpdate()
	{
		Vector2 velocity = new Vector2(0, _movementSpeed);
		_rigidbody.velocity = velocity;
	}
	
	private void OnDestroy()
	{
		OnBalloonDestroyed?.Invoke();
	}
}

public class BalloonSpawner : MonoBehaviour
{
	[SerializeField]
	private ScoreHandler _scoreHandler;
	
	[SerializeField]
	private Balloon _balloonPrefab;
	
	[SerializeField]
	private float _spawnInterval;
	
	private float _currentTime;
	
	private void Update()
	{
		_currentTime += Time.deltaTime;
		if (_currentTime >= _spawnInterval)
		{
			SpawnBalloon();
			_currentTime -= _spawnInterval;
		}
	}
	
	public void SpawnBalloon()
	{
		Vector2 position = //work out spawn position
		var balloonInstance = Object.Instantiate(_balloonPrefab, position, Quaternion.identity);
		balloonInstance.OnBalloonDestroyed += HandleOnBalloonDestroyed; // subscribe to delegate
	}
	
	private void HandleOnBalloonDestroyed()
	{
		_scoreHandler.IncreaseScore(amount: 1);
	}
}

public class ScoreHandler : MonoBehaviour
{
	[SerializeField]
	private TMP_Text _scoreText;
	
	private int _score = 0;
	
	private void Awake()
	{
		UpdateScoreText();
	}
	
	public void IncreaseScore(int amount)
	{
		_score += amount;
		UpdateScoreText();
	}
	
	public void UpdateScoreText()
	{
		_scoreText.text = $"Score: {_score}";
	}
}

Mostly code to give you an idea of the concept. The BalloonSpawner and ScoreHandler components can probably go on the same empty game object in the scene. Haven’t included anything about lives/clicking, etc, as mostly want to demonstrate how you would use multiple components to handle separate responsibility and how to communicate between them.

You’re learning and there’s no shame in that. No one knows these things first time around. Just requires some patience to work through the tough early days. It only gets easier with time.

1 Like

Thanks a lot for your help and patience ! I’ll meditate on this :smiley:

1 Like