- Home /
Accessing other script problem
I have 2 scripts; Characterdamage2.js & spawn.js I want to get a variable in CharacterDamage2 and use it in spawn. Pretty much I want the variable "hitPoints" in CharacterDamage2 to +=10 with every wave in the spawn script but I don't know what I'm doing wrong.
I used this; http://docs.unity3d.com/356/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
And got this;
var CharacterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2); CharacterDamage2.hitPoints += 1.0();
Nobody helped me with this code on my old question (http://answers.unity3d.com/questions/537671/static-var-wont-work.html)
That is some pretty weird syntax in that code snippet right there. Looks like a mix of unityscript and c++ syntax? I'm not sure what you're actually trying to do here.
Anyway, GetComponent is your friend! You shouldn't be declaring CharacterDamage2 as a variable, since it already exists as a typename. Try something like this:
var myDam : CharacterDamage2;
myDam.hitPoints += 1;
Also, that's a strange name for a class. Any reason for the number 2?
The reason for number 2, is this is kind of a $$anonymous$$i game im currently working on, so I want it separate than the real game
Yes, that link you have posted twice includes all the information you need to solve your problem. It also has nothing even close to that code snippet! I am really not sure what part of it is confusing you.
Answer by Pecek · Sep 18, 2013 at 12:23 PM
Details! The snippet says
var otherScript: OtherScript = GetComponent(OtherScript);
otherScript.DoSomething();
And you are trying to do it this way:
var OtherScript: OtherScript = GetComponent(OtherScript);
OtherScript.DoSomething();
It's similar, but not the same. The variable in the example uses the variable "otherScript", and a script named "OtherScript". Your code should work fine if you use a different name for your variable, like this:
var characterDamage2: CharacterDamage2 = hitpoints(CharacterDamage2);
characterDamage2.hitPoints += 1.0;
But I would use the solution of syclamoth, just to be sure not do any typos later.
The world really needs to settle on using camelCase for variables and PascalCase for classes and such. I know the .NET world is in love with PascalCase for everything, but my brain just can't get over it.. and most online syntax hilighters assume PascalCase is a classname too.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Static var wont work 2 Answers
What is the problem with this code? 1 Answer
How to access variable from another function? 2 Answers
Why are my variables not showing up? 2 Answers