Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ybce · Mar 31, 2017 at 06:04 PM · monobehaviourunity 4

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.

alt text

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.

brick.png (8.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ybce · Mar 31, 2017 at 07:19 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges