Problem in enemySpawner.cs

Aloha!

After 2 days of trying to figure out why, i still dont know what is the cause for this error.

the error description is:|‘Object’ does not contain a definition for ‘GetComponent’ and no extension method ‘GetComponent’ accepting a first argument of type ‘Object’ could be found (are you missing a using directive or an assembly reference?)

Looks like its something wrong with “var newEnwmy” since it (if this “newEnemy” is referred as ‘Object’ in error description) does not contain a definition for ‘GetComponent’??

Also when im typing: “newEnemy.Getco…” I get NO autocomplete option for whole statement, like Rick does in lecture…

I dont know how to fix this…I would appreciate any help i can get :slight_smile:

Hello Janez,

The Instantiate method returns an object of type Object, it doesn’t contain a method named GetComponent, this is primarily why you are getting your error message.

If you cast your returned object to a type of GameObject you should then find the method GetComponent is available to you.

// ...

GameObject newEnemy = (GameObject)Instantiate(waveConfig.GetEnemyPrefab(), waveConfig.GetWayPoints()[0].transform.position, Quaternion.identity);

// ...

The cast is in the brackets before the Instantiate method, equally you could add as GameObject at the end of the statement instead.

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

[Solved]

Hello Rob,

Thank you for clearing things up a bit. This did the trick for me.

Still, it bugs me a bit…why do i have to use this different way of instantiating this newEmeny game object, than rick did in the lecture. Is this due to different unity versions?

Hi Janez,

Glad to hear this has resolved the problem for you.

With regards to why you need to do it differently, I’d probably need to see your project files and then compare against the course version of this project, of course you could do this yourself. I would suggest it would come down to the specific types that are being referenced, I can’t tell from your screenshot above for example what the type of the object is that is returned by waveConfig.GetEnemyPrefab().

It’s probably unlikely to be related to the version of Unity you are using, but as I don’t know what that is, again, it would be challenging to say definitively.

Hope this helps :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms