- Home /
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.
Why not have a ReceiveData(Data data) function and send the appropriate object the data?
because I don't know how to do the "appropriate object" part
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.
one specific card
and if not SendMessage, what else?
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);
}
Your answer
Follow this Question
Related Questions
Sending a message from javascript to C# 1 Answer
Button is unable to grab functions with two parameters? 0 Answers
Public functions not calling and scripts not being set to active 0 Answers
Unity hangs on start when finding gameobject 0 Answers
I'm having problems with bools resetting within a function 0 Answers