Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rcsjk07 · Nov 20, 2021 at 04:02 AM · functionsendmessagevoidfunction call

How to activate a function (or equivalent) in a specific gameobject

I am trying to send data from Shuffle into GooCardScript. GooCardScript will be found within 5 different GameObjects, but I only want one of them to receive the data. From what I can tell, SendMessage and BroadcastMessage do not differentiate between objects.

Here is the script that sends data:

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using UnityEngine;
 
 public class Shuffle : MonoBehaviour
 {
     // calls my data objects
     public SpawnDeckList SpawnDeckList;
     public SpawnManagerScriptableObject SpawnManagerScriptableObject;
 
     // temp variables
     public bool[] GooCard = new bool[5];
 
     // varibles for deck sorting
     int TopCard = 0;
     int GooEmpty = 5;
 
     // Start is called before the first frame update
     void Awake()
     {
         // temp variable definations
         GooCard[0] = true;
         GooCard[1] = true;
         GooCard[2] = true;
         GooCard[3] = true;
         GooCard[4] = true;
 
         // code here to decide what decks are chosen
 
         // merging the decks the plays choose into Deck
         int[] Deck = new int[SpawnDeckList.SpindleDeck.Length + SpawnDeckList.SpindleDeck.Length];
         Array.Copy(SpawnDeckList.SpindleDeck, Deck, SpawnDeckList.SpindleDeck.Length);
         Array.Copy(SpawnDeckList.SpindleDeck, 0, Deck, SpawnDeckList.SpindleDeck.Length, SpawnDeckList.SpindleDeck.Length);
 
         // shuffling Deck
         int tempDeck;
         for (int i = 0; i < Deck.Length; i++)
         {
             int rnd = UnityEngine.Random.Range(0, Deck.Length);
             tempDeck = Deck[rnd];
             Deck[rnd] = Deck[i];
             Deck[i] = tempDeck;
         }
 
         // making sure nothing is broken
         Debug.Log("first cards that appear in the goo are numbers ");
         for (int i = GooEmpty; i > 0; i -= 0)
         {
             Debug.Log(Deck[TopCard]);
             MakeCard(Deck[TopCard]);
             TopCard += 1;
             GooEmpty -= 1;
             i -= 1;
         }
         Debug.Log("TopCard is " + TopCard);
         Debug.Log("GooEmpty is " + GooEmpty);
     }

     // *this is the important part*
     // *this is the important part*
     // *this is the important part*

     // make the card with the stats
     void MakeCard(int CardID)
     {
         if (GooCard[0] == false)
         {
             // insert code to message GooCard1 with the info
         }
         else if (GooCard[1] == false)
         {
             // insert code to message GooCard2 with the info
         }
         else if (GooCard[2] == false)
         {
             // insert code to message GooCard3 with the info
         }
         else if (GooCard[3] == false)
         {
             // insert code to message GooCard4 with the info
         }
         else if (GooCard[4] == false)
         {
             // insert code to message GooCard5 with the info
         }
         else
         {
             Debug.LogError("<color=red>Error:</color> Either all GooCards are full and the GooEmpty was mistakenly called, or at least one of the GooCards are listed as full when they aren’t");
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }

The script receiving data:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GooCardScript : MonoBehaviour
 {
     // calls my data objects
     public SpawnDeckList SpawnDeckList;
     public SpawnManagerScriptableObject SpawnManagerScriptableObject;
     private SpawnManagerScriptableObject.Card PersonalStats;
 
     // temp variables
     public bool[] Card = new bool[5];
     public int Player = 1;
 
     // Start is called before the first frame update
     void Start()
     {
         // temp variable definations
         Card[1] = true;
         Card[2] = true;
         Card[3] = true;
         Card[4] = true;
     }

     // the function I want to activate
     void ReciveStats(SpawnManagerScriptableObject.Card Stats)
     {
         Stats = PersonalStats;
         // insert code to apply sprite and cost
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             if (Player == 1)
             {
                 if (Card[0] == false)
                 {
                     // insert code to message Card1 with the stats
                 }
                 else if (Card[1] == false) 
                 {
                     // insert code to message Card2 with the stats
                 }
                 else if (Card[2] == false) 
                 {
                     // insert code to message Card3 with the info
                 }
                 // so on and so forth
                 else
                 {
                     Debug.LogError("<color=red>Error:</color> Either I messed up the script or you somehow have 120 cards on one side and you want more, if the ladder is true, I quit");
                 }
             }
         }
     }
 }

Notes / Clarifcation:

The GooCardScript will save the data, then send the data to a different GameObject later. Those GameObjects will all have the same script and I plan to use the same method I receive here. There will be 240 of these GameObjects, so it will be a long a tedious process to make a unique script with unique functions for each one.

Comment
Add comment · Show 6
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 unity_ka6jgzfPPmtNCw · Nov 20, 2021 at 01:24 PM 0
Share

Why not have a ReceiveData(Data data) function and send the appropriate object the data?

avatar image rcsjk07 unity_ka6jgzfPPmtNCw · Nov 20, 2021 at 03:18 PM 0
Share

because I don't know how to do the "appropriate object" part

avatar image logicandchaos rcsjk07 · Nov 20, 2021 at 03:29 PM 0
Share

what do you want to send?

Show more comments
avatar image Bmarlyman21 · Nov 20, 2021 at 06:11 PM 0
Share

Are you trying to send the data to one specific card, or can it be any of them?

This may be an inefficient way of solving the problem, but you could make a class-level boolean that checks whether you have sent the data or not. Then before sending the data, you check if that boolean is false, then send the data and set it to true.

I wouldn't recommend using SendMessage in most cases, because if you ever scale up the project, that would make changing method names very difficult (you would need to find every instance of SendMessage and change it). Instead, you could create a method on the receiving object and call that directly.

avatar image rcsjk07 Bmarlyman21 · Nov 20, 2021 at 09:08 PM 0
Share

one specific card

and if not SendMessage, what else?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bmarlyman21 · Nov 20, 2021 at 10:02 PM

If you're wanting to send data to a specific card, I would do 2 things. First, implement the suggestion that @unity_ka6jgzfPPmtNCw made and add a new function to your GooCardScript called "TransferData" (or whatever you like), then add to the parameters whatever data you want to send. So something like this:

 void TransferData(Data data)
 {
     print ("Received " + data);
 }

Second, add a new variable in your Shuffle script which stores all of your GooCardScript objects. From there, if you want to send the data to a particular card, call that card's TransferData function. So let's say you wanted to send the data to card 3 (keeping in mind that the number starts at 0, this would make card 3 the fourth card in the array), it might look like this:

 GooCardScript[] cards = new GooCardScript[5] // You can also use a List instead of an array
 
 void Start()
 {
     card[3].TransferData(data);
 }
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

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sending a message from javascript to C# 1 Answer

Calling a function in another script not working with yield 1 Answer

Child object collision 2 Answers

Full list of predefined voids? 2 Answers

is it possible to pass functions through functions? 2 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