I’ve seen some references to this topic, but no solutions that match so far.
I am simply trying to trigger an animation when my game ends and I continually get this error. It’s based straight off of the “Penelope” tutorial so I don’t know why it’s failing, further I plugged in almost the same function in my playerCollision script so that when enough things are collected the “WIN” animation is triggered.
However it is failing to do so here and gives me this error with nary a clue to what it means.
help?
Object reference not set to an instance of an object
My script:
private function EndGame()
{
var animationController : AnimationController = GetComponent( AnimationController );
var prefab : GameObject = Instantiate(guiMessage);
var endMessage : GUIText = prefab.GetComponent( GUIText );
//Player LOSE ANIMATION!!!
endMessage.text = “Oh no…You lose!”;
PlayAudioClip( loseSound, Vector3.zero, 1.0 );
animationController.animationTarget.Play( “LOSE” );
yield WaitForSeconds(1);
// Alert other components on this GameObject that the game has ended
SendMessage( “OnEndGame” );
Application.LoadLevel(“GameOver”);
while( true )
{
// Wait for a touch before reloading the intro level
yield WaitForFixedUpdate();
if ( iPhoneInput.touchCount > 0 iPhoneInput.GetTouch( 0 ).phase == iPhoneTouchPhase.Began )
break;
}
}
Note in the above the error is on the line that should play the “LOSE” animation. I can swap out this for other animations (which definitely work) and it still does not work).
Hi, I just had the wait in there so that the game doesn’t end too abruptly, and so that ideally it had time enough to play my LOSE animation.
I’ve read through the Penelope.pdf again and I don’t see why my script is failing still.
By any chance, were you asnwering to the post I just deleted ?
After thought, I was unsure of your problem, so I delete it 
I reread your topic, and I think about it again.
You say you have an error message, which line ?
I get the error when it goes to play the animation, but I think I’ve found where I’m having a problem.
Okay how does this sound. I set up my guis, and then attached my scorekeeper to the gui to make the temperature bar rise as you go through the game, and end the game when it gets too hot… etc.
However, as with all things Unity, you attach scripts to things logically. The logic here seemed to me to be to attach it to the Gui displaying the temperature but I’m trying to play an animation on the Player.
I just checked Penelope again and the Scorekeeper is attached to the Player not to the Gui.
So I think I’m on the right track here, and this explains why my PlayerCollision where i use the same function for WIN is working and the LOSE is not.
It’s not attached to the right game objects. I’ll have to go through my files and see if I can sort this out cleanly without breaking things… but I’ll post if it works.
Okay, this is in fact the problem which explains the error. Unity couldn’t understand what it was supposed to access for the animation because I had the scoreKeeper script attached to a gui rather than the character.
My animation is finally working for the end of game when you lose.
Of course on the down side I had to detach my gui for the temperature gauge and now have to figure out what I need to do to get this working again and working correctly with ending the game as well.
At least I’m on the right track.
ack.
I’m back where I started from. I applied the ScoreKeeper to my Player, and this means when I’m out of time it ends the game appropriately and plays the appropriate animation.
However, I still can’t get my gui to communicate properly. The gui tracks the temperature in the game, and tries to send a signal to the ScoreKeeper to end the game however since the endGame() is a private function it cannot.
When I make it a static function I get all the same annoying instancing problems with the function.
It’s making me batty. I’m not used to this behavior, I’m used to making something a global function and accessing it from another script so I don’t really understand what is going on here or how to resolve this.
Why do you need to set the function private ? ^^’
I don’t know why the function is private, mind you I built this from parts of the Penelope tutorial.
I tried to make it static, to share between scripts but it gives me a load of errors, those same errors I have trouble figuring out what to do with at this point:
ERRORS
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(167,57): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘GetComponent’.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(167,57): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘GetComponent’.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(168,47): BCE0020: An instance of type ‘ScoreKeeper’ is required to access non static member ‘guiMessage’.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(178,9): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘SendMessage’.
Assets/Daev_Assets/DV_Scripts/ScoreKeeper.js(178,9): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘SendMessage’.
Why don’t you make it public ?
I’ve tried that. I’ve tried making them static. I’ve tried making the functions that call out to them the same as what I switch them to, and not.
I don’t understand enough about how the private/public/static is working to understand why it’s failing.
But to be clear, when I switch it to static and public I get errors, the ones I listed above.
I know this has something to do with the fact that it’s accessing a function that makes calls to the animationController, but I don’t know how to fix it at this point.
Hum, if you can upload your project folder, I could give it a try (if it do not require Unity Iphone to look at it !).
That’s a generous offer, but it is iPhone and I don’t feel its’ the best practice to send entire projects out to others for feedback.
To connect the two, have a variable like this in the script that calls the temperature script:
var myTemp : ScriptName;
ScriptName would be what you called your temperature script in the project folder. Using the above variable, you can access non-private variables like this:
myTemp.myVar
and functions like this:
myTemp.myFunc();
Ack. I got it to work.
It occurred to me that although I had attached the ScoreKeeper to my character it wasn’t quite done. I had to incorporate the temperature functions into the scorekeeper properly.
Then create a variable that is in score keeper and point my guiTexture there.
Not a great explanation, but it works. Animations work. Scripts are cleaner. I’m not trying to access across scripts for something that just needed to have a variable in the scorekeeper, like timerGui might be a variable in scorekeeper.
whew.
thanks to everyone who helped and I’ll try to summarize better later.
You just need to define the object as a public variable in your script.
i.e.
var MyObject : GameObject
Then it will show up in the inspector and you can simply drag the relevant object onto it to make the link.
Essentially you are right and that is what I did, realized I needed to have my gui just referenced in my scorekeeper script so I could plug it in. I used the line:
var temperatureGui : GUITexture ;
Then I had all the logic to make the gui grow (it’s basically like a health gui that grows) in the same ScoreKeeper script.
The problem originally stemmed from my mistake of adding the logic to the gui, which worked in tracking the temperature and would end the game, but it wouldn’t trigger the character animation to end the game.
If I had just built this in my original interface gui I would’ve figured it out without giving myself grief.