- Home /
Adding a script to a prefab
I have a prefab brick object and this object gets destroyed during game play. I want to detect every time a brick gets destroyed so I can keep track of how many bricks are left on screen. I decided to create a BreakerController object and declare it publicly in the Brick.cs script. I've done this before and all I had to do was drag the BreakerController object into that space and it would link the two together.
However, this time it is simply refusing to link them. I tried linking it when I'm focused on an instance of the prefab and it worked fine but when I clicked apply it still wouldn't accept the change and apply it to all the prefabs.
Brick.cs
public class Brick : MonoBehaviour {
public int max_hits;
private int times_hit;
public BreakerController breakerController;
// Use this for initialization
void Start () {
times_hit = 0;
}
void OnCollisionEnter2D(Collision2D collision){
times_hit++;
if (times_hit == max_hits) {
breakerController.detectDestruction();
Destroy(this.gameObject);
}
}
// Update is called once per frame
void Update () {
}
}
BreakerController.cs
public class BreakerController : MonoBehaviour {
public LevelManager levelManager;
public int score = 48;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (score == 0) {
levelManager.LoadLevel("WinScene");
}
}
public void detectDestruction(){
score --;
}
}
This is code I'm trying to write but I can't seem to link the BreakerController object to the Brick script. I am very new to game design and I appreciate any tips or better ways to achieve my goal, I just felt that this might be a good way to do this.
Thanks.
Answer by Hanoble · Mar 31, 2017 at 06:23 PM
It sounds like you are trying to link a prefab to a scene object. A prefab exists in the project outside of a scene, so when you try to link a prefab to a scene object and save the prefab you will see it "not linked" in the project, but those in scene will have the link.
Think of the prefab as a template and the instance you create as an actual brick which uses that prefab template to know what the brick should be, essentially a clone of the prefab (this is why you will see (clone) after prefab instances). You cannot link the controller, which exists in the scene, to a template object that exists in the project and could be in ANY scene. However, IF those brick instances are in the same scene as the controller you CAN then because they are both actual instances that exist together.
What you will most likely want to do is whenever you create a brick, go ahead and assign the controller in that chunk of code. For example in your Brick Start() do:
Start()
{
breakerController = GameObject.Find("BreakerControllerObjectNameHere").GetComponent<BreakerController>();
}
There are many other ways you can establish that link in scene between the brick and the controller, some are much more efficient than others, but that should be enough to get you started.
@Hanoble thank you so much for your answer I ended up selecting all the instances and linking them to make it work. Your solution is much more elegant, thanks again for your answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612114118im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
XML Serialization problem 0 Answers
Are namespaces supported in Unity 4? 5 Answers
How to make IslandDemo water more reflective 1 Answer
Missing Push Notification Entitlement (invalid binary) 0 Answers
Unity 4.5 Text 0 Answers