Do you need the ‘var’ keyword before the first instance of scoreGoalScript? Is this the whole code file or is scoreGoalScript already defined elsewhere?
var scoreGoalScript = GameObject.Find("GoalCollide").GetComponent(ScoreGoal);
var currentScore = scoreGoalScript.currentScore;
var finalScore = (currentScore * GameController.scoreMultiply);
I don’t know the specifics of how JS works in these cases I am afraid.
It’s optional (but implied) when defining a variable, but extremely good practice to use it. Actually, I kinda wish UT would make it non-optional. (It would break a ton of scripts I’m sure, so maybe some kind of #pragma thingy…)
I don’t see anything wrong with that code as-is. There aren’t any other errors, or anything? And like Jeremy said, is scoreGoalScript already defined elsewhere?
It is defined elsewhere. It is (as far as I know) defined right above it like so.
var scoreGoalScript;
It was
var scoreGoalScript = GameObject.Find("GoalCollide").GetComponent(ScoreGoal);
which gave this error:
The only thing I can think of is that some sort of ambiguity is going here, so the compiler either thinks
a.) scoreGoalScript isn’t a variable
which means it may think
b.) It’s a name of a class or
c.) It’s a name of a function
I don’t know. Maybe declaring the variable with the type ScoreGoal (var scoreGoalScript : ScoreGoal;) to make sure the compiler knows what type the variable is would help?
That’s the first place it is. And when I put “var” in front I of the first line I get an error that states …
Unity Exception: You are not allowed to call this function when declaring a variable.
This sort of thing makes me long for a coding language like X-Talk (e.g., in Hypercard or Runtime Revolution) where I can program in English and actually understand the darn code. Or tear my remaining hair out entirely (although that would be a short, futile exercise over very quickly).
PS. Thanks Jedd for helping out this forever idiot.
I’m going to watch Heroes now (the only TV I watch) then go to sleep to get less angry/depressed over what should be a trivial matter. I sure as hell am not cut out for this coding stuff. I sometimes wonder why I even try, even with help like is here on the forums. Just upset I gues today. I’ll tackle this again tomorrow sometime.
You are far too hard on yourself, and you are expecting to be able to figure out everything.
There are always little simple snags that kill a lot of time. The trick is to just keep going. Tenacity wins through every time.
Something else you should know, it’s actually the simple problems that are usually the hardest to figure out. In most coding projects, the large scale stuff usually doesn’t present many problems (aside from taking time to write), but it’s the little tiny one-liners that bring it all to a halt. Some mis-named variable deep in the code, a missed cast or something.
As long as you take your time, don’t let yourself get frustrated and stay focused on it, you will figure it out. Don’t sell yourself short, and I recommend you see every coding issue as one more thing you have discovered. Most times you have to do something wrong 2,000 times before you come to the right solution.
You can do it if you want to.
Another point, you’ll notice no one else has figured out your problem yet either, so it’s not just you.
Also, you said in your first post: “How can I be such a noob after two + years???” IF you look back at what you could do 2 years ago compared to now you will be surprised. believe me, and either way, this stuff takes time to learn. It’s not always intuitive at first.
Ok, ok, preaching is over, I just hate it when people sell themselves short. They are locking out their full potential that way.
QFT, as they say. Your code is correct, so you do know what you’re doing; there’s just something going wacky somewhere. I’d suggest trying what Jedd said:
var scoreGoalScript : ScoreGoal = GameObject.Find("GoalCollide").GetComponent(ScoreGoal);
If that doesn’t work, try renaming ScoreGoal to something else and changing the script(s) to match, just in case the name is colliding with something, somehow.
Also, make sure you’re not pressuring yourself too much. I remember you saying something about having this done by November (I think), which obviously isn’t happening, so don’t worry about it. It takes as long as it takes…how many stories do we read about software going years past the deadline and millions of dollars over budget? (Well, OK, try not to hit that goal, anyway…)
Had a good sleep. Will look at this after I get home and I’ve done playing with the kids and they’re in bed. Great community here. Mustn’t be too hard on self.
I’m guessing that this code is at the top level of the JS file rather than inside a method?
You can probably do without the GameObject.Find call. Declare your variable withvar scoreGoalScript: ScoreGoal; …and you should see it appear in the editor. If you now drag the GoalCollide object onto this variable in the inspector, it should set up the value you want in a way the editor can live with.
The reason for the problem is pretty subtle, by the way - it’s not just a noob-trap.
Nothing worked. But due to Jedd’s persistance he managed to wrangle this one to the ground by by wrapping the whole thing in a function, and then using the function name to get the score into a GUI box element, as shown here …
function GetFinalScore()
{
var goalCollide = GameObject.Find("GoalCollide");
var scoreGoalScript : ScoreGoal = goalCollide.GetComponent(ScoreGoal);
var currentScore = scoreGoalScript.currentScore;
var finalScore = (currentScore * GameController.scoreMultiply);
return finalScore;
}
so that the score displays further down the code as …
// a field to show player final score with multiplier here
GUI.Label (Rect (50,70,200,20), "Your score = " + GetFinalScore());
Back to sanity by standing on the shoulders of programming giants.