Glitch in my Glitch Gardens


#1

I don’t know what happened but not only did I somehow mess up my trophy’s animation last lecture but not this lecture, for whatever reason, my number display does not go to 100. It stays at 999. I rechecked my code and everything looks right to me.

public class StarsDisplay : MonoBehaviour {

    [SerializeField] int stars = 100;
    Text starText;

	void Start () {
        starText = GetComponent<Text>();
        UpdateDisplay();
	}
	
    private void UpdateDisplay()
    {
        starText.text = stars.ToString();
    }
}

#2

Hi Penny,

There’s not really enough information here to help you with the animation issues, but regarding the stars, looking purely at the code you’ve given, the only value stars should have right now would be 100, as it isn’t being set anywhere else in your code to a different value and then the starText reference just gets continuously updated.

As a complete guess, being that you stated the value is 999, that sounds to me like a value you have set in the Inspector for the text component on the UI Text GameObject. If this isn’t changing to 100 which this script should make it do, I would suggest perhaps you’ve not added this script as a component to the UI Text GameObject in question.

Really quick/easy way to test the theory, in your Start method, add a line at the top of it like this;

Debug.Log("StarsDisplay script is currently attached to : " + gameObject.name);

When your game runs, if this script is attached to a GameObject the console will display this message and the name of which GameObject it is attached to. If you don’t see the message at all, you’ve not attached the script.

You could also right-click on the script within Assets and then use the Find References in Scene option, this will then filter the Hierarchy to show only relevant GameObjects that either have the script attached, or are associated with it in some way (like a wired up OnClick event for a button etc).

Hope this helps :slight_smile:


#3

Hey Rob,

Here is a screenshot of what shows up after adding the Debug.Log line:


#4

Hi Penny,

With those four errors, the top two are fairly straight forward to resolve.

Open your animation window and select your trophy, you may need to drop one into the scene temporarily to do this.

Select the clip from the drop down menu (top-left of the animation window) and on the timeline to the right you’ll note that you have one (or more) animation events, the appear as a small white vertical bar, blue if selected.

Select each one and then in the Inspector on the right you’ll see a drop down menu for the function to associate with the event.

You either have not selected the function which you meant to, or, added an event you didn’t need. You can now either select the appropriate function or, if you don’t need the event, with it selected press delete (I think you can right-click on it for the delete option also).

After resolving these, let’s see what happens with the other two null reference exception errors and go from there.

Sorry for lack of screen shots, replying via mobile.


Updated Sun Dec 09 2018 11:06

Screenshots to make it easier to explain;

  • GameObject selected in Hierarchy on the left

  • Animation window open

  • “Player_Idle” (in this case, not Glitch Garden) Animation Clip selected

  • On the right, small white vertical bar on the timeline, that’s the Animation Event

  • Select the Animation Event, it will turn blue, and view the Inspector, you’l see the “No Function Selected” message in the drop down menu.
    image

  • Either expand and select a function, e.g.;
    image

  • Or, right-click on the Animation Event and select Delete Animation Event if you don’t want it;
    image

Hope this helps :slight_smile:


#5

Ok, I went ahead and deleted where it said “no function selected” so that error went away. This is the error that I still have remaining:

It keeps pointing to this line in my code but I don’t know why:

~private void SpawnDefender(Vector2 roundePos)
{
Defender newDefender = Instantiate(defender, roundePos, Quaternion.identity) as Defender;
Debug.Log(roundePos);
}
~


#6

I did go back a few lessons until I could see what the code should look like there and I did notice that I it should have said ‘roundedPos’ rather than ‘roundePos’ but still that did not change anything as far as errors.


#7

Hi Penny,

Whilst I cannot see the full details from the error in your screenshot, it is mentioning you instantiating an object, which I think is your clue here. It is saying that you have tried to instantiate something, but the something that you were referencing was null. If I had to make an educated guess here I’d suggest that was where you have a script exposing a prefab field, the thing that will be instantiated, but perhaps you’ve forgotten to wire that up in the Inspector. As such, when the Instantiate method is called, the variable you pass in holds a reference to null instead of to the prefab that you wanted to create an instance of.

As you’ve mentioned the specific code it points to, I would suggest its perhaps the defender here that is the issue, there is likely to be a exposed field that isn’t pointing to the correct prefab to instantiate. Try your different defender buttons and see which one causes the error.