• 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 simcity · Mar 16, 2017 at 08:32 PM · networkgetcomponent

getcomponent doesn't work with networkbehaviour,Getcomponent doesn't work with NetworkBehaviour

hello i'm having problem with getcomponent i'm trying to get the bool variable from wasdcontroller to movestat but it doesn't work. It does work with opposite direction like movestat to wasdcontroller.i'm not good at coding so the code will be litte dirty

wasdController_Net

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class wasdController_Net : NetworkBehaviour
 {
 
     public float myspeed;
     public bool tell_Sprint;
     public float starve;
     public float dehydrationNum;
     public bool _Sprint;
     public bool Push_wasd;
 
     // Use this for initialization
     void Start()
     {
         if (!isLocalPlayer)
         {
             Destroy(this);
             return;
         }
         myspeed = 3;
         tell_Sprint = false;
         _Sprint = true;
     }
 
     void Update()
     {
         Movement();
         decrease_speed();
         Sprint();
     }
 
     void Sprint() 
     {
         starve = GameObject.FindWithTag("player").GetComponent<movestat>().hunger;
         dehydrationNum = GameObject.Find("Player").GetComponent<movestat>().dehydration;
         if ((Input.GetKeyDown(KeyCode.LeftShift) && starve > 29) || (Input.GetKeyDown(KeyCode.LeftShift) && dehydrationNum > 29))
         {
             myspeed += 3f;
             tell_Sprint = true;
             _Sprint = true;
         }
         if ((Input.GetKeyUp(KeyCode.LeftShift) && starve > 29 && _Sprint == true) || (Input.GetKeyUp(KeyCode.LeftShift) && dehydrationNum > 29 && _Sprint == true))
         {
             myspeed -= 3f;
             tell_Sprint = false;
         }
         if ((starve < 30 && _Sprint == true && tell_Sprint == true && myspeed > 3) || (dehydrationNum < 30 && tell_Sprint == true && _Sprint == true && myspeed > 3))
         {
             myspeed -= 3f;
             _Sprint = false;
             tell_Sprint = false;
         }
     }
 
     void decrease_speed()
     {
         if ((starve <= 29.00 && starve > 0) || (dehydrationNum <= 29.00 && dehydrationNum > 0))
         {
             myspeed = 1f;
         }
         if ((myspeed <= 0.01f && myspeed > 0.00 && starve > 30) && (myspeed <= 0.01f && myspeed > 0.00 && dehydrationNum > 30))
         {
             myspeed = 3f;
         }
     }
 
     void Movement() 
     {
         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.W))
         {
             Push_wasd = true;
             float moveAmount_V = myspeed * Input.GetAxis("Vertical") * Time.deltaTime * -1;
             float moveAmount_H = myspeed * Input.GetAxis("Horizontal") * Time.deltaTime;
             transform.Translate(moveAmount_V, moveAmount_H, 0f);
         }
         else
         {
             Push_wasd = false;
         }
     }
 }
 

movestat

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class movestat : wasdController_Net
 {
     public float hunger;
     public float dehydration;
     public bool Sprint_hunger;
     public bool Move_hunger;
 
 
     private void Awake()
     {
 
     }
     // Use this for initialization
     void Start ()
     {
 
         hunger = 100;
         dehydration = 100;
 
     }
 
     void Update ()
     {
         information_reciver();
         hunger_sub();
         hydrate_sub();
     }
 
     void information_reciver()
     {
         Sprint_hunger = GameObject.FindWithTag("player").GetComponent<wasdController_Net>().tell_Sprint;
         Move_hunger = GameObject.FindWithTag("player").GetComponent<wasdController_Net>().Push_wasd;
     }
 
     void hunger_sub() 
     {
         hunger -= 0.0005f; 
         if (Move_hunger)
         {
             hunger -= 0.005f;
         }
         if (Sprint_hunger)
         {
             hunger -= 0.005f;
         }
     }
 
 
     void hydrate_sub() 
     {
         dehydration -= 0.0008f; 
         if (Move_hunger)
         {
             dehydration -= 0.007f;
         }
         if (Sprint_hunger)
         {
             dehydration -= 0.007f;
         }
     }
     
 }
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

0 Replies

· Add your reply
  • Sort: 

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

UNet: How to get isLocalPlayer from a parent 1 Answer

Unity 5 Adding a script component to another object upon Enter 0 Answers

Very strange problem - networking related. 1 Answer

Unity Best Practices - Cached Components 1 Answer

Parenting over network 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