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
1
Question by chillypacman · Oct 01, 2011 at 11:56 AM · c#prefabgame-object

Sending data to different game objects?

I have a bit of a design problem here and not sure how I should solve this problem in Unity.

My game has a scoreboard in the form of a tk2DSprite, it exists in the game scene and is positioned wherever I want to see it.

The score is meant to be incremented everytime a scriptually generated game object (created through a prefab and the clone function) is destroyed.

My problem is that I'm not sure how the game object should 'talk' to the scoreboard, I obviously can't add it to the prefab script as an object so I'm more or less out of ideas here..

I was thinking if I could figure out a way to query the scene and grab all the objects in it I could find the scoreboard that way, but that's probably not how I'm supposed to do it.

So any ideas?

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
2
Best Answer

Answer by Tseng · Oct 01, 2011 at 01:00 PM

Well, I personally like doing it with the C# delegate/events and a Singelton Object for the score.

 public sealed class Score {
     private static readonly Score instance = new Score();
     private int score;
     public delegate void ScoreUpdatedEventHandler(int newScore);
     public static event ScoreUpdatedEventHandler ScoreUpdate;    
     
     public Score() {
         score = 0;
     }

     public static Score Instance {
         get { return instance; }
     }
     public static Score GetInstance() {
         return instance;
     }
     
     public int Score {
         get { return score; }
     }
 }

Since you don't need to Find and obtain the script with a singleton, you just need Score.Instance.Score += 10 to increase the score by 10. In order to be notified when the score gets updated you'd need to place a script into your score Sprite, which will listen to it.

     Score.Instance.ScoreUpdate += new delegate(int newScore) {
         // Do whatever is necessary on score update here
     }

This is pretty handy as you can register an unlimited number of events to listen to score updates, i.e. Achievement updates and the checks will only be performed if the score was updated and not every frame.

Of course you can also do this inside of your generated objects Awake() Method

 private Score score;
 void OnAwake() {
     GameObject o = transform.Find("/ScoreObjectInHierarchyView");
     if(o!=null) {
         score = o.GetComponent<Score>();
     }

     if(score==null)
         Debug.LogError("Couldn't find Score Script");
 }

But I really hate this approach, is much of hustle to add it every time and the delegate one is pretty easy and keeps the code clean

edit: You also don't have a problem [with the event/delegate] if you rename the script object inside of the hierarchy view, that's where the above code would fail.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do you save a reference to a prefab's script in a ScriptableObject? 0 Answers

Open up a dialogue window via right click 1 Answer

Accessing component of a single prefabs' instance. 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