Ok, so you have a couple of things going on here…
The error message is being generated by the compiler, it is unhappy because you are trying to use a type which doesn’t exist, e.g. when you say “Find A” but you don’t even have an “A” in the project. It would be like us meeting in a barn full of hay and me asking you to find the jibber-wockey, but not telling you what a jibber-wockey at is or looks like… you would just look at me like I was crazy - this error is the compiler doing that to you
The next thing I notice is that you have this;
// Use this for initialization
void Start () {
GameObject.FindObjectOfType<Snitch>();
}
On it’s own, that is going to do very little, because what it does (or doesn’t find) isn’t be put anywhere.
Really, you’d want something like this;
// Use this for initialization
void Start ()
{
Snitch snitch = GameObject.FindObjectOfType<Snitch>();
}
In the above, we define a local (to the method) variable named “snitch”, this will hold the reference to whatever the GameObject.FindObjectOfType<Snitch>()
statement returns.
If we have a GameObject in the scene with our Snitch.cs class attached as as script component, happy days, snitch
will reference that GameObject and we could perform other operations upon it.
If however we do not, then snitch
is going to be null
, if we were then to try and perform other operations upon it we would receive a nullreferenceexception
error.
We could handle that with a couple of minor changes;
// Use this for initialization
void Start ()
{
Snitch snitch = GameObject.FindObjectOfType<Snitch>();
if(snitch)
{
// snitch returns true, thus we found something
Debug.Log("Well done Harry! You got the Snitch!");
}
else
{
// snitch returns false, we found nothing
Debug.Log("Dumbledore is displeased with your efforts Harry!");
}
}
Hope this helps
I’m having a lot of fun stretching my brain with this course.
Awesome