• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by telamon2525 · Apr 04, 2015 at 08:47 PM · nullreferenceexceptioncastingroguelike

Roguelike2d example Walls fail to cast correctly in Player:OnCantMove

Hey guys,

got the tutorial working on my PC running windows 7 professional, unity 5.0.0.f4. everything works, food counts down, character and enemies animate correctly, enemies attack player and players animations and enemies also work. The problem is the internal walls: they are supposed to take damage, and in the OnCantMove of player, when its a wall thats passed in, when I try and cast it in Player:OnCantMove:

protected override void OnCantMove (T component)

     {  

         //Set hitWall to equal the component   
 
                     //passed in as a parameter.   

         if (component != null) {   

                             // test works and the gameObject exists, so its actually there  

             GameObject test = component.gameObject;  

  

               if (test.name.StartsWith("Wall")) {  

                 
                 Wall hitWall = component as Wall;
                             // now hitWall on the other hand here
                             // is null in the debugger

=========== Its basically an exact copy of all scripts from the completed section, and everything else works. So why does hitWall return null when I attach to the process and check on it during runtime. I even tried converting Wall to a MovingObject, and after adding the appropriate code (so wall could move, but in this case doesn't), I get the exact same error: than in Debug, when it gets to Player:OnCantMove, when the player is attempting to move into a wall, the casting to Wall fails and hitWall is null.

Also note in the OnCantMove of Enemy the Player is picked up and cast correctly: Player hitPlayer = component as Player;

this line actually casts the Player correctly, and both animate, and the player takes damage.

Any suggestions? This code is driving me crazy!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by telamon2525 · Apr 07, 2015 at 01:01 AM

OK, so far, I've got enemies working and dying. Now I've added a new prefab called house, and I instantiate them on the map, and they have their own animation controllers, and they don't move as of yet, and I can destroy them. The only issue I have is on the AttemptMove in Player script I have to add another attemptmove line:

 AttemptMove<Enemy>(horizontal, vertical);
 AttemptMove<House>(horizontal,vertical);

and then do cases for them in the OnCantMove of Player based on the name of the object passed in. Which is silly for so many reasons. My question is this: do I have to have everything inherit from Enemy, and then do subset code of each enemy which could include things like moving walls, stationary walls, lava flows, etc? Or is there some way to call AttemptMove in Player with

 AttemptMove<T>

such that it figures out the type (as you'd figure C# generics should) and then do the appropriate damage to said enemy, or talk to said NPC?

Let me know

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by telamon2525 · Apr 07, 2015 at 12:45 AM

So: some important facts about C# generic functions:

1) when attached with the debugger and debugging through the script at runtime, you can tell whether you have a correct object passed of Type T if, for example the object is a Player, then it will looks like this:

So click the play button, then open up a script (so MonoDevelop opens), the click on the Menu Run->Attach, then click on attach to process.

In Enemy.cs, go to the OnCantMove function and add a breakpoint right after retrieval of the component line:

Player hitPlayer = component as Player;

now play through until Level 3 and wait for an enemy to swing at you. Note that when he does your screen should look like this in the watch value:

(note put two breakpoints is safer, then hit F10 to step over that assignment line. Then the value should be:

alt text In the OnCantMove of player I've changed the attemptmove line to: AttemptMove(horizontal, vertical);

That way the player can now attack and kill enemies (I've had to add hit points to enemies too, but thats particularly easy adding a public int hp and adding a function to enemy to damage himself called DamageEnemy.

Now that works: however when I pass in a wall, because its the wrong time in AttemptMove in player (which I've now set to Enemy, it fails. And a failure looks like this:

alt text

Now you see how it says Wall6 Clone and then UnityEngine.transform? Thats the debugger's subtle way of saying the object failed to pass correctly. So my question is this: should I pass some sort of list here? Or just make all objects enemies, so that I can destroy walls if they are of type enemy, etc etc. The only problem I have with that later is adding NPCs (non player characters), and then having some interaction with them doesn't make sense to place them in the 'Enemy' category.

so would it be best to somehow have a list to interact with? Or can I do an AttemptMove from player and have it cast correctly when I attack a wall?

Let me know. I hope this question helps with clarity on debugging Template passing functions... and I hope someone out there has a solution for me :)


validtemplatepass.png (25.1 kB)
failedtopasstemplate.png (40.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Null reference exception with elusive cause 1 Answer

Gameobject not null at start but in function it is. 2 Answers

NullReferenceException by localRotation And Quaternion.Euler!?? 1 Answer

Null Reference Exception when using variable in another script 2 Answers

Scene Null Reference in build 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges