1
\$\begingroup\$

There are lots of instantiated prefabs. Info from it is transmitted while it crosses a trigger zone. Some of these are calculated custom variables.

I can print the custom variables from the instantiated prefabs.

I can get the trigger zone to report standard things like "...transform.position" or "...transform.localEulerAngles", but not stuff I make up. Intellisense seems to see everything I am trying to get, and everything compiles.

What's the best manner to perform this? Here's what I've tried:

Prefabs' Script:

public float infoToSend;
 void Update(){
 float infoToSend = 10f //...actually, there's some math here, but it's a float.
}
print("Sender: " + infoToSend); //Works.

Receiver Script on Trigger Zone:

void OnTriggerStay2D(Collider2D other){

print("Receiver: " + other.transform.position);  //Works Fine.
print("Receiver: " + other.transform.localRotation);  //Works Fine.

float infoReceived = other.GetComponent<PrefabsScript>().infoToSend;
print("Receiver: " + infoReceived);  //Does not work!
}

Any help, either fixing this arrangement or directing me to a different method, would be appreciated. Thanks.

Edit to display the solution:

Corrected Prefabs' Script (changed third line down):

public float infoToSend;
 void Update(){
 this.infoToSend = 10f //...actually, there's some math here, but it's a float.
}
print("Sender: " + infoToSend); //Works.

Correct Receiver Script on Trigger Zone:

void OnTriggerStay2D(Collider2D other){

print("Receiver: " + other.transform.position);  //Works Fine.
print("Receiver: " + other.transform.localRotation);  //Works Fine.

float infoReceived = other.GetComponent<PrefabsScript>().infoToSend;
print("Receiver: " + infoReceived);  //Does not work!
}

Many thanks, again! There's so many places to begin with, it's tough to find a "Beginner course" to cover everything.

\$\endgroup\$
  • \$\begingroup\$ Have you considered using Events or Getter Methods for this type of publisher/subscriber relationship? \$\endgroup\$ – DMGregory Apr 8 at 1:10
  • \$\begingroup\$ the fact you declare another float inside update of infoToSend would overwrite the class one, and mean you dont get 10f out of it.. \$\endgroup\$ – BugFinder Apr 8 at 9:35
1
\$\begingroup\$
public float infoToSend; // This is a public member variable, also called a field.

void Update(){
    float infoToSend = 10f; // This is a local variable that exists only in this function.
}

When you start a variable assignment with a type, like float, you're telling the compiler, "hey, I'm defining a new variable of type float here. So, forget about any member variables I might have defined with that name for the time being - as long as we're inside this Update() method, the name infoToSend will refer to this local variable instead."

(If you did this deliberately, and you want to refer to the infoToSend at member scope instead of the local infoToSend in the method's scope, you can say this.infoToSend to specify that you're talking about the member variable)

So, in this case, you've never assigned the value of 10 to your public field. You assigned it only to a local variable, that gets thrown out as soon as the function completes (you'll get a warning from the compiler about this, if you've assigned a variable without ever using it anywhere).

This is called variable shadowing, and it's a fundamental part of the language syntax that's not specific to game development. If you're unfamiliar with the basics of C# syntax, then it might be best to brush up with some introductory tutorials before proceeding.

If you just want to assign a value to a variable you've previously declared, you don't start the line with the type - the compiler already knows the type from its original declaration. So you can just say:

infoToSend = 10f;

This says "assign the value of 10 to my existing variable infoToSend, without declaring a new variable at this scope."


The foregoing fixes the particular code you wrote, but it's generally considered poor programming practice to expose everything as public variables. When they're public, any code anywhere could modify them, so if you get a bug from the variable holding the wrong value, it's more challenging to track down where it came from. We say techniques like this "increase the surface area for bugs."

So, if you have a strict relationship where your prefab object publishes this data, and other scripts consume it, you should enforce that relationship in your code.

A classic way is to make a getter function:

float infoToSend;
public float GetInfo() { return infoToSend; }

Now only this script can change infoToSend's value, while anyone can read it. This is so popular, C# has a shorcut for writing this kind of relationship, through what are called "auto properties":

public float infoToSend {get; private set;}

This makes a publicly readable variable that only the local script can set. It's really just syntactic sugar on a private setter / public getter method, but it's short to type and makes your intention clear.

You could also use Events to send this type of information when it's updated - a push relationship instead of a pull - adding or removing subscribers as needed.

\$\endgroup\$
  • \$\begingroup\$ This gives me a few avenues to study more thuroughly. It's tough to find an answer when you don't really know what the question is! Thanks! \$\endgroup\$ – ScumSprocket Apr 8 at 11:08
  • \$\begingroup\$ Then please update your post to include your actual code. We can only help you when you show us the real problem. Otherwise, it's garbage in, garbage out. Better to make a minimal complete verifiable example you can test to be sure it demonstrates the problem, and paste that exact code, than to share fictionalized code that might not capture the real issue. \$\endgroup\$ – DMGregory Apr 8 at 12:43
  • \$\begingroup\$ Wait! "This." Did make it work! I thought it was just removing the extra float declaration. Wow... I thought this. was just a thing that existed inside a method, if you spell it out in the argument... I know THAT must sound newbish. \$\endgroup\$ – ScumSprocket Apr 8 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.