Glitch in my Glitch Gardens

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();
    }
}

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:

Hey Rob,

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

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:

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);
}
~

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.

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.

Hey Rob,

It doesn’t matter what I click on. As soon as I click the mouse, the error pops up.

This is what the complete error looks like :
ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:381)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:206)
UnityEngine.Object.Instantiate[Defender] (.Defender original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:285)
DefenderSpawner.SpawnDefender (Vector2 roundedPos) (at Assets/Scripts/DefenderSpawner.cs:37)
DefenderSpawner.OnMouseDown () (at Assets/Scripts/DefenderSpawner.cs:11)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

Hi Penny,

It does look heavily related to the instantiation of a GameObject. Best I can do at this point is to offer to take a look at the project if you would like to share your project files?

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Hey Rob,

Sorry I haven’t gotten back to you. I have been so bogged down with work lately that I haven’t had a chance to even focus on my coursework (which I miss). I have had to work extra hours with Christmas coming up and trying to scrimp and save for a planned trip. I hope to get back on track though really soon. I may have to reorient myself or either restart on Glitch Gardens. Also, my computer has really been giving me a headache on my job. For some reason, it gets so bogged down that it won’t even load up a page that I’m working on. I have tried to cleaning it up, deleting files, etc. but so far none of that has helped very much. I think it may possibly have something to do with my work programs that I have to use but I’m not sure. I even added another course to my course workload that I thought might help me to figure out how to speed up my computer but the one who is doing that course is not really teaching me anything that I don’t already know and the course is really pretty short.

Hope you and your family had a lovely Christmas. When I get back to where I can focus on Glitch Gardens again, I will give you a holler. You really are good at helping people like me out. Thanks so much!

1 Like

Hi Penny,

That’s not a problem at all and I fully appreciate the demands on your time and focus, especially at this time of year.

If you’ve not got too far into the Glitch Garden section then restarting it when you’re ready may be a great idea, you won’t have to worry about trying to remember what you had done/where you’d got to, and as you progress you’ll most likely recall some of the content so it will be a smoother journey second time round.

Sorry to hear about the computer issues, I’m not sure whether you were referring specifically to Unity or just in general, but hopefully you’ll get them sorted.

Thank you for the kind Christmas wishes and I look forward to seeing you return to the course when you’re ready. As for my help, you are very welcome :slight_smile:

Privacy & Terms