• 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 Punkjim420 · Feb 05, 2014 at 08:48 PM · c#onlinesend

[Closed]Send health to player targeted on item use, C#, online

Im trying to use a potion and have my target heal but when i use my potion the players do not heal, the debug.log's are called though.. so i dont know whats wrong with my code.

Player.CS:

//!!!this works!!!//

 if (networkView.isMine)
         {
             InputMovement();
             //InputMeshChange();
             GetRay();
             if (Input.GetKeyDown(KeyCode.F)){
                 playerHp = playerHp + 1;
             }
         }
 if (target && Input.GetKey (KeyCode.Mouse0) && target.tag == "Entity" && attackTimer <= Time.time && target != this.gameObject){
                 if (networkView.isMine){
                     target.networkView.RPC("TakeDamage", RPCMode.All, 1);
                 }
                 attackTimer = Time.time + 0.4f;
 
         }

Inventory.CS:

//!!This does not work!!//

 [RPC]public void TakeHeal(int value){
         playerScript.playerHp = playerScript.playerHp + value;
         playerScript.playerHp = Mathf.Clamp (playerHp,0,playerMaxHp);
         Debug.Log ("takeheal reached");
     }

the addition never gets called/is not updating/or is not remaining updated long enough for me to see it. but the debug "takeheal reached" works. so TakeHeal() is being called for sure. question is, why does it not update the playerHp?

heres the script i use to call TakeHeal():

 void useItem(int mySlot){
         if(contents[mySlot] == potion){
             playerScript.TakeHeal(100);
             Debug.Log ("worked");
             if(contents.Count <= 49){
                 contents.Add (hiPotion);
                 Debug.Log ("added pot");
             }
         }

this is in the inventory script, its debugs are also called, yet once again the other things dont happen, but it does happen if i play single player. im trying to make it work online also. if i play online, i see the debugs but the hipotion never gets added to my List named "contents". this function DOES however reach the TakeHeal function of my player script, because the debugs all go off properly.. any suggestions?

my player is the same prefab for all players, yet each one works on networkView and works flawlessly except for this attempt to "send" my useitem function between scripts. ive made sure that my target was my own player prefab with a debug, and im definately targeting the correct object..

Comment
Add comment · Show 5
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 Punkjim420 · Feb 07, 2014 at 12:53 PM 0
Share

bump, im still having this problem, if more code is needed i can post more. Also if i need to be more specific i can and will do so.

avatar image Punkjim420 · Feb 07, 2014 at 03:57 PM 0
Share

ok, ill do that now.

avatar image Punkjim420 · Feb 07, 2014 at 04:14 PM 0
Share

I think i may be onto something, my value is always 100 as intended, and my health does not increase, i noticed some funny behavior though, if i open and close the inventories of other players from within the editor, it changes my hp in the debug, and even in the game sometimes, i think the inventory script is using every player in game rather than one, i will have to do some testing on this to find out.. any other suggestions are definately welcome.

avatar image Punkjim420 · Feb 07, 2014 at 04:30 PM 1
Share

Narv if you convert your comment to an answer, ill accept it, your advice did help me find the problem..so i think you deserve it. it was the fact that my players are all running on networkView and i did not use my inventory on networkView with them, ins$$anonymous$$d all players were interacting with the same inventory booleans(i only saw the booleans changing because you advised a check on the playerHp and value :P) and such, causing unwanted behavior. :)

avatar image Narv · Feb 07, 2014 at 09:58 PM 0
Share

glad to be of service. Debugging everything is awlays fun. :) Converted comment to answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Narv · Feb 07, 2014 at 03:49 PM

Try adding a Debug.log of the values in the function too to make sure that the value 100 is being passed as value. and player health before and after the addition to see if it's adding properly.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

When online GUI.Label (or text area etc) shows overlapping text 2 Answers

What scripting language should I use for a multiplayer project? 1 Answer

Store Data Online 0 Answers


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