Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 AgarFun · Jul 04, 2017 at 02:49 PM · networkinginstantiatenetworkhealth barhealth

Instantiate health bar with networking

When a bullet hit an asteroid, the health bar decrease of 10 if the player is only local, so how Instantiate the healthbar on the server, this is my scripts : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;

public class AsteroidController025 : NetworkBehaviour {

 [SerializeField]
 private StatA asteroidHealth025;

 

 private void Awake()
 {

     asteroidHealth025.Initialize();

 }

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Bullet")
     {
         
         asteroidHealth025.CurrentVal -= 10;
         Destroy(other.gameObject);
     }

 }

 // Use this for initialization
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
     if (asteroidHealth025.CurrentVal <= 0)
     {
         Destroy(this.gameObject);
     }
 }

} and

using System; using System.Collections; using UnityEngine; using UnityEngine.Networking;

[Serializable] public class StatA { [SerializeField] private ABarScript aze;

 [SerializeField]
 private float maxVal;

 [SerializeField]
 private float currentVal;


 public float CurrentVal
 {
     get
     {
         return currentVal;
     }

     set
     {

         this.currentVal = Mathf.Clamp(value, 0, MaxVal);
         aze.Value = currentVal;
     }
 }

 public float MaxVal
 {
     get
     {
         return maxVal;
     }

     set
     {

         this.maxVal = value;
         aze.MaxValue = maxVal;
     }
 }
 public void Initialize()
 {
     this.MaxVal = maxVal;
     this.CurrentVal = currentVal;
 }

   

}

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 Shrikky23 · Jul 04, 2017 at 03:00 PM

Okay I see what is going on. First I will assume you know how Unity Networking works (How the Sync between player objects work, what is network behavior etc)

  1. To Solve your specific problem, I noticed that your StatA script is not inheriting from Network behavior, which means it cannot run any network specific functionalities in the script itself. A script has to inherit from NetworkBehavior and have a NetworkIdentity component to be spawned in the network as you wanted. If you use NetworkServer.Spawn (called in the server/host), then one instance of the object will be spawned in all the player's scenes and it will have a NetworkInstanceID attached and other properties like "isLocalPlayer", "HasAuthority?" etc, however, the ownership of the object belongs to the person who spawned it, in our case it would be server/host.

  2. There are cool Unity Network attributes like [SyncVar] it is used to synchronize variables across its instances in the network, which means if you add [SyncVar] above private float currentVal; it will synchronize its value automatically across all the network players. You must also understand that only the player who spawned the Object can control or change its Values (Look more into Object Authority/ who has the right to modify values)

Unity Networking is fun and time consuming at the same time. I kept going back to read networking documentation so often to understand how Command/RPC/ Local Authority/ Synchronization of variables using SyncVar, SyncEvent and how all of that works.

I hope my explanation gives a good start for your problem.

Comment
Add comment · Show 2 · 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 AgarFun · Jul 04, 2017 at 03:28 PM 0
Share

So, I can't sync the health bar on all screen ? The sync var don't works, when it's on, the healthbar has a value = 0. if you can, send me the script changed please. And I forget the last script : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class BarScript : $$anonymous$$onoBehaviour {

 private float fillAmount;

 [SerializeField]
 private float lerpSpeed;

 [SerializeField]
 private Image content;

 [SerializeField]
 private Text valueText;

 public float $$anonymous$$axValue { get; set; }

 public float Value
 {
     set
     {
         string[] tmp = valueText.text.Split(':');
         valueText.text = tmp[0] + ": " + value;
         fillAmount = $$anonymous$$ap(value, 0, $$anonymous$$axValue, 0, 1);
     }
 }

 // Use this for initialization
 void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     HandleBar();    
 }
 private void HandleBar()
 {
     if (fillAmount != content.fillAmount)
     {
         content.fillAmount = $$anonymous$$athf.Lerp(content.fillAmount, fillAmount, Time.deltaTime * lerpSpeed);
     }
     
 }

 private float $$anonymous$$ap(float value, float in$$anonymous$$in, float in$$anonymous$$ax, float out$$anonymous$$in, float out$$anonymous$$ax)
 {
     return (value - in$$anonymous$$in) * (out$$anonymous$$ax - out$$anonymous$$in) / (in$$anonymous$$ax - in$$anonymous$$in) + out$$anonymous$$in;
 }

}

avatar image Shrikky23 AgarFun · Jul 07, 2017 at 12:48 AM 0
Share

$$anonymous$$ake your fillamount a syncvar? So networking debugging is fun, change sync var and see if it displays on your other player's screen. If it changes, then all you have to do is apply the new fill amount to the content.fillAmount using your handlebar function

avatar image
0

Answer by TrEVoR2004 · Jul 13, 2017 at 05:38 PM

https://youtu.be/GfuxWs6UAJQ

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.

Follow this Question

Answers Answers and Comments

121 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game works when i use "build and run" and in the game tab, but not when i build it as an EXE 0 Answers

[UNET] How to know the connectionID of a player ? 2 Answers

Network.Instantiate is CHAOS. 1 Answer

How to create a object that can only be seen by one player? 0 Answers

Instantiate prefab and passing parameter 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