Search Unity

Unknown identifier scoreGoalScript??? What am I doing wrong?

Discussion in 'Scripting' started by DaveyJJ, Dec 4, 2007.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Code (csharp):
    1.  
    2. scoreGoalScript = GameObject.Find("GoalCollide").GetComponent(ScoreGoal);
    3. var currentScore = scoreGoalScript.currentScore;
    4. var finalScore  = (currentScore * GameController.scoreMultiply);
    5.  
    This line

    var currentScore = scoreGoalScript.currentScore;

    is giving me "Unknown identifier scoreGoalScript" error ... what am I doing wrong?????

    How can I be such a noob after two + years???
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Do you need the 'var' keyword before the first instance of scoreGoalScript? Is this the whole code file or is scoreGoalScript already defined elsewhere?

    Code (csharp):
    1.  
    2. var scoreGoalScript = GameObject.Find("GoalCollide").GetComponent(ScoreGoal);
    3. var currentScore = scoreGoalScript.currentScore;
    4. var finalScore  = (currentScore * GameController.scoreMultiply);
    5.  
    I don't know the specifics of how JS works in these cases I am afraid.

    Hope that was somewhat useful. ;-)

    -Jeremy
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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?

    --Eric
     
  4. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Speaking for DaveyJJ here :)

    It is defined elsewhere. It is (as far as I know) defined right above it like so.
    Code (csharp):
    1. var scoreGoalScript;
    It was
    Code (csharp):
    1. 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?
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    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.
     
  6. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Well David, I have something to say to that!

    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. :)

    -Jeremy
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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:

    Code (csharp):
    1. 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....)

    --Eric
     
  8. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    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.
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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 with
    Code (csharp):
    1. var 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.
     
  10. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    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 ...

    Code (csharp):
    1.  
    2. function GetFinalScore()
    3. {
    4.     var goalCollide = GameObject.Find("GoalCollide");
    5.     var scoreGoalScript : ScoreGoal = goalCollide.GetComponent(ScoreGoal);
    6.     var currentScore = scoreGoalScript.currentScore;
    7.     var finalScore = (currentScore * GameController.scoreMultiply);
    8.     return finalScore;
    9. }
    10.  
    so that the score displays further down the code as ...

    Code (csharp):
    1.  
    2.     // a field to show player final score with multiplier here
    3.     GUI.Label (Rect (50,70,200,20), "Your score = " + GetFinalScore());
    4.  
    Back to sanity by standing on the shoulders of programming giants.