- Home /
Can 2 scripts attached to one object communicate with each other?
In UnityScript.
ok say i have magic points in my players status script(health, magic, strength, expirience etc), would i be able to write to ask my players status script what the magic variable current is from inside my abilities script? i want to search for my magic points amount, and if i have enough, allow the abilities coded inside of my abilities script. BOTH SCRIPTS are on ONE OBJECT.
i just dont know if i have to actually "search" for the variable "magicPoints" or if itll already know, sense both scripts are parts of a single rigidbody object.
if i can do this, what methods are possible?
@Fattie: I've often deleted whole discussions which of course were off topic after we cleared the facts. Every comment with useful information will be kept ;)
Answer by Bunny83 · Aug 24, 2012 at 04:50 PM
Sure, if the abillities script needs to access those variables more than once, you should setup the reference in start:
private var playerStatus : NameOfYourPlayerStatusScript;
function Start()
{
playerStatus = GetComponent(NameOfYourPlayerStatusScript);
}
function Update()
{
if (playerStatus.magic > 20)
// [...]
}
You know, I usually do this in Awake()
(because "I might need to use it in Start()," or, someone else might need to use it early),
but I don't know if I'm making a morally sound decision.
Sure, Awake is fine in most cases, but in some it won't work. For example when you attach the script manually with AddComponent at runtime. Awake is called even before the AddComponent call returns. If both scripts need to cross reference each other it won't work in Awake. I usually prefer Start but, like you said, it depends on your initialization philosophy and general structure.
thanks, this is a great answer.
Its one of the few that not only showed what i should code to get the result i want, but it also is simple enough to understand what the coding means and that allows for other usage when attempting to make something other than this specific thing. most tutorials dont explain what they are doing. its more like "do this and this will happen."
"Awake is called even before the AddComponent call returns" .. indeed, a great point.
In a word, if you are attaching scripts at runtime (perhaps at set-up-time), it won't work in Awake().
Thanks !
Answer by Mander · Aug 24, 2012 at 04:57 PM
1.global variables.(i don't recommend this)
declare as static so u can use it.
for instance script, name: script1,script2
script 1: variable declaration
public static int magicDamage;
script 2: variable usage
damage = script1.magicDamage * spellPower;
////////////////
2.get as components
script 1: variables public int magicDamage = 20;
script 2: int magicDamage;
declare variables normaly not static and just ask for the variables with GetComponent
and use them
magicDamage = GetComponent<script2>().magicDamage;
damage = magicDamage * spellPower;
you probably wouldn't really do this dude. you just access the other class.
Also, unlike most other questions, this one clearly says in UnityScript. I'm also a C# user, but when you post examples, at least add what language it's written in, or it wouldn't be much help for new users.
Also it seems you turned your whole answer into a code block. That also doesn't help to understand what you're doing there. Either only marks code as code or turn your text into inline comments.
well. im just tryin to help. it doesn't hurt to use the doc to "translate" from c# to unityscript. helping ppl too much is bad too, else they will try to get all their scripts from UAnswers. just saying
Thanks for your input, its always great help to find a coder who explains what they are doing rather than just pasting code snippets. In method 2 you used GetComponent to access variables from the script you searched for. I understand, and prefer GetCompnent and shall use that method.
my pleasure man. im just glad to help and i belive that if i explain the code somehow ppl will get the idea and try to implement it them selves in other scripts rather than ask again.