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 J-R-Wood · Nov 05, 2017 at 03:35 PM · scripting problemnetworkinglocal

SetActive for objects on a local server

Well i have searched everything on anything about this. i have Unity4 because i cannot afford to buy the next version(not sure if that changes anything)

Am i supposed to use RPCs or SyncVar? My code is for my player i have a gameobject for each animation(i know that is not traditional) so for running there is a gameobject with an animation for running that is set active during a particular event only problem is other players can't see this change. if someone could point me in the right direction. Thanks in advance.

I'm gonna show you guys my code but don't make fun #pragma strict

 var Speed : float; 
 var NEGSpeed : float; 
 var Speedx10 : float;
 var JumpDis : float;
 var JumpDis2 : float;
 var Run : GameObject;
 var nRun : GameObject;
 
 var JP : boolean;
 
 
 var CanAnim : boolean;
 
 
 //@SyncVar
 //public var AnimationLists : int;
 
  
  
 var StabAnimation : GameObject;
 var ShootAnimation : GameObject;
 var IdleHands : GameObject;
 var ShieldAnimation : GameObject;
 
 var Switch : int;
 var bow : boolean;
 var melee : boolean;
 var shield : boolean;
 var build : boolean;
 
 var Ammo : GameObject;
 var Cube : GameObject;
 var AmmoAmount : int;
 var CubeQAmount : int;
 var FirePoint : GameObject;
 
 
 function Start () {
 
 }
 
 
 function OnTriggerEnter(col : Collider)
 {
 if (networkView.isMine)
 {
 if (col.tag=="Mooshroom")
 {
 Jumping2();
 }
 
 
 if (col.tag=="Ammo")
 {
 AmmoAmount += 10;
 }
 }
 }
 @RPC
 function Update () {
 
 NetworkView.RPC(
 OnSerializeNetworkView
 
 if (networkView.isMine)
 {
  if (Input.GetKey ("w"))
  {
  transform.Translate(Vector3.forward * Speed);
 
  Run.SetActive(true);
  }
   if (Input.GetKey ("w") && Input.GetKey (KeyCode.LeftShift))
  {
   Run.SetActive(true);
 
  transform.Translate(Vector3.forward * Speedx10);
 
   
  }
    if (Input.GetKeyDown ("space") && JP)
  {
 Jumping(); 
  JP = false;
  }
  
  if (!Run)
  {
   Run.SetActive(false);
  }
  
  if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
  {
   Run.SetActive(true);
   nRun.SetActive(false);
  }else{
   nRun.SetActive(true);
   Run.SetActive(false);
  }
  
  
   if (Input.GetKey ("a"))
  {
   transform.Translate(Vector3.right * NEGSpeed);
  }
   if (Input.GetKey ("s"))
  {
   transform.Translate(Vector3.forward * NEGSpeed);
  }
   if (Input.GetKey ("d"))
  {
   transform.Translate(Vector3.right * Speed);
  }
  
  
  
  
    if(Input.GetKeyDown(KeyCode.Tab))
  {
  CycleWeapons();
  }
  
    if(Input.GetKeyDown(KeyCode.LeftControl) && shield)
  {
 shielding();
  }
  
  if (CanAnim)
  {
  IdleHands.SetActive(true);
  }else{
  IdleHands.SetActive(false);
  }
  
  if (Input.GetButtonDown("Fire1") && bow && AmmoAmount > 0)
  {
  Network.Instantiate(Ammo, FirePoint.transform.position, FirePoint.transform.rotation,0);
 shooting();
  }
  
  if (Input.GetButtonDown("Fire1") && build && CubeQAmount > 0)
  {
 
  Network.Instantiate(Cube, FirePoint.transform.position, FirePoint.transform.rotation,0);
  shooting();
  }
  
  if (Input.GetButtonDown("Fire1") && melee && CanAnim)
  {
  stabbing();
  }
 
 }
 }
  function CycleWeapons ()
  {
  if (networkView.isMine)
  {
  Switch-=1;
  if (Switch <=0)
  {
  Switch =4;
  }
  
  if (Switch==1)
  {
  bow = true;
  melee = false;
  build = false;
  shield = false;
  }
  if (Switch==2)
  {
  bow = false;
  melee = true;
  build = false;
  shield = false;
  }
  if (Switch==3)
  {
  bow = false;
  melee = false;
  build = true;
  shield = false;
  }
  if (Switch==4)
  {
  bow = false;
  melee = false;
  build = false;
  shield = true;
  }
  }
  }
 
 function shooting ()
 {
 if (networkView.isMine)
 {
 CanAnim=false;
 ShootAnimation.SetActive(true);
 yield WaitForSeconds(0.25);
 ShootAnimation.SetActive(false);
 CanAnim=true;
 }
 }
 
 function stabbing ()
 {
 if (networkView.isMine)
 {
 CanAnim=false;
 StabAnimation.SetActive(true);
 yield WaitForSeconds(0.30);
 StabAnimation.SetActive(false);
 CanAnim=true;
 }
 }
 
 function shielding ()
 {
 if (networkView.isMine)
 {
 CanAnim=false;
 ShieldAnimation.SetActive(true);
 yield WaitForSeconds(5);
 ShieldAnimation.SetActive(false);
 CanAnim=true;
 }
 }
 
 
 
 
 
 function Jumping()
 {
 if (networkView.isMine)
 {
  rigidbody.AddForce(transform.up * JumpDis);
 yield WaitForSeconds(1);
 JP = true;
 
 }
 }
 
 
 
 function Jumping2()
 {
 if (networkView.isMine)
 {
  rigidbody.AddForce(transform.up * JumpDis2);
 }
 }
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
1

Answer by RisingDead_xTR · Nov 05, 2017 at 04:16 PM

You can upgrade to Unity 2017.2 for FREE. It seems like u r using PUN (which i dnt use, i use UNet) . So I can't gv u an exact code but the concept. Here is a code to do it in UNet(jst in case).

 void Start()
     {
         GameObject Object;
         CmdDisableObject (Object);
     }
 
     [Command] //Command functions are sent to the server from client to run it there
     void CmdDisableObject (GameObject obj)
     {
         RpcDisableObject(obj); //Telling the server to tell all clients(including me) to disable the 'obj'.
     }
 
     [ClientRpc] //ClientRpc funtions are sent to all clients from server to run it there
     void RpcDisableObject(GameObject obj)
     {
         obj.SetActive (false); //Server tells all clients to disable this 'obj'.
     }

Concept: I (client) am telling the server to tell all clients to disable the 'obj'. Hope it helps. Edit: I just read tht u need to enable objects. However i think u cn do it from the concept.

Comment
Add comment · Show 3 · 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 J-R-Wood · Nov 05, 2017 at 05:47 PM 0
Share

So for JavaScript could I do var idle : Boolean; var run : Boolean; var fight : Boolean; var FightState : GameObject; var WalkState : GameObject; var IdleState : GameObject; function start() { //TESTIING Idle.enabled=true fight.enabled=true run.enabled=true } @ClientRpc function PlayerState () { If (run) { WalkState.SetActive(true); FightState.SetActive(false); IdleState.SetActive(false); } If (idle) { WalkState.SetActive(false); FightState.SetActive(false); IdleState.SetActive(true); } If (fight) { WalkState.SetActive(false); FightState.SetActive(true); IdleState.SetActive(false); } }

avatar image J-R-Wood · Nov 05, 2017 at 05:48 PM 0
Share

I’m at church but I’ll try testing it later on Unity

avatar image J-R-Wood · Nov 05, 2017 at 06:47 PM 0
Share

So i was trying this on my script and its spitting out an error when i checked their manual it shows this public class Example extends NetworkBehaviour {, do i need to add it to my code somehow too?

avatar image
0

Answer by J-R-Wood · Nov 06, 2017 at 04:40 AM

I have now got it working if anybody was wondering. the new code looks like this...

 #pragma strict
 
 
 var Speed : float; 
 var NEGSpeed : float; 
 var Speedx10 : float;
 var JumpDis : float;
 var JumpDis2 : float;
 var Run : GameObject;
 var nRun : GameObject;
 
 var JP : boolean;
 
 
 var CanAnim : boolean;
 
 
  
 var StabAnimation : GameObject;
 var ShootAnimation : GameObject;
 var IdleHands : GameObject;
 var ShieldAnimation : GameObject;
 
 var Switch : int;
 var bow : boolean;
 var melee : boolean;
 var shield : boolean;
 var build : boolean;
 
 var Ammo : GameObject;
 var Cube : GameObject;
 var AmmoAmount : int;
 var CubeQAmount : int;
 var FirePoint : GameObject;
 
 
 
 public var NetAnimState : int;
 
 
 
 
 
 
 
 
 
 
 function Start () {
 
 }
 
 
 function OnTriggerEnter(col : Collider)
 {
 if (networkView.isMine)
 {
 if (col.tag=="Mooshroom")
 {
 Jumping2();
 }
 
 
 if (col.tag=="Ammo")
 {
 AmmoAmount += 10;
 }
 }
 }
 
 
 
 
 function Update () {
 
 
 if (networkView.isMine)
 {
  if (Input.GetKey ("w"))
  {
  transform.Translate(Vector3.forward * Speed);
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,1);
  Run.SetActive(true);
  }
   if (Input.GetKey ("w") && Input.GetKey (KeyCode.LeftShift))
  {
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,1);
   Run.SetActive(true);
 
  transform.Translate(Vector3.forward * Speedx10);
 
   
  }
    if (Input.GetKeyDown ("space") && JP)
  {
 Jumping(); 
  JP = false;
  }
  
  if (!Run)
  {
   Run.SetActive(false);
  }
  
  if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
  {
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,1);
   Run.SetActive(true);
   nRun.SetActive(false);
  }else{
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,2);
   nRun.SetActive(true);
   Run.SetActive(false);
  }
  
  
   if (Input.GetKey ("a"))
  {
 //  transform.position = Vector3.Lerp(transform.position, realPosition, 0.1f);
   transform.Translate(Vector3.right * NEGSpeed);
  }
   if (Input.GetKey ("s"))
  {
   transform.Translate(Vector3.forward * NEGSpeed);
  }
   if (Input.GetKey ("d"))
  {
   transform.Translate(Vector3.right * Speed);
  }
  
  
  
  
    if(Input.GetKeyDown(KeyCode.Tab))
  {
  CycleWeapons();
  }
  
    if(Input.GetKeyDown(KeyCode.LeftControl) && shield)
  {
 shielding();
  }
  
  if (CanAnim)
  {
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,6);
  }
  
  if (Input.GetButtonDown("Fire1") && bow && AmmoAmount > 0)
  {
  Network.Instantiate(Ammo, FirePoint.transform.position, FirePoint.transform.rotation,0);
 shooting();
  }
  
  if (Input.GetButtonDown("Fire1") && build && CubeQAmount > 0)
  {
 
  Network.Instantiate(Cube, FirePoint.transform.position, FirePoint.transform.rotation,0);
  shooting();
  }
  
  if (Input.GetButtonDown("Fire1") && melee && CanAnim)
  {
  stabbing();
  }
 
 }
 }
  function CycleWeapons ()
  {
  if (networkView.isMine)
  {
  Switch-=1;
  if (Switch <=0)
  {
  Switch =4;
  }
  
  if (Switch==1)
  {
  bow = true;
  melee = false;
  build = false;
  shield = false;
  }
  if (Switch==2)
  {
  bow = false;
  melee = true;
  build = false;
  shield = false;
  }
  if (Switch==3)
  {
  bow = false;
  melee = false;
  build = true;
  shield = false;
  }
  if (Switch==4)
  {
  bow = false;
  melee = false;
  build = false;
  shield = true;
  }
  }
  }
 
 function shooting ()
 {
 if (networkView.isMine)
 {
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,4);
 }
 }
 
 function stabbing ()
 {
 if (networkView.isMine)
 {
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,3);
 }
 }
 
 function shielding ()
 {
 if (networkView.isMine)
 {
 networkView.RPC("MyStateOnNetwork", RPCMode.Others,5);
 }
 }
 
 function Jumping()
 {
 if (networkView.isMine)
 {
  rigidbody.AddForce(transform.up * JumpDis);
 yield WaitForSeconds(1);
 JP = true;
 }
 }
 function Jumping2()
 {
 if (networkView.isMine)
 {
  rigidbody.AddForce(transform.up * JumpDis2);
 }
 }
 
 
 
 
 
 
 
 @RPC
 function MyStateOnNetwork(NetAnimState : int)
 {
 if (NetAnimState==1)
 {
 nRun.SetActive(false);
 Run.SetActive(true);
 }
 if (NetAnimState==2)
 {
 nRun.SetActive(true);
 Run.SetActive(false);
 }
 if (NetAnimState==3)
 {
 CanAnim=false;
 StabAnimation.SetActive(true);
 yield WaitForSeconds(0.30);
 StabAnimation.SetActive(false);
 CanAnim=true;
 }
 if (NetAnimState==4)
 {
 CanAnim=false;
 ShootAnimation.SetActive(true);
 yield WaitForSeconds(0.25);
 ShootAnimation.SetActive(false);
 CanAnim=true;
 }
 if (NetAnimState==5)
 {
 CanAnim=false;
 ShieldAnimation.SetActive(true);
 yield WaitForSeconds(5);
 ShieldAnimation.SetActive(false);
 CanAnim=true;
 }
 if (CanAnim && NetAnimState==6)
  {
  IdleHands.SetActive(true);
  }else{
  IdleHands.SetActive(false);
  }
 }
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

156 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 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

Alternative to RPC for destroying objects across a network 0 Answers

Networking (messaging a variable to remote player on hook) 2 Answers

what are the basic rules off making a lan server 1 Answer

Assigning Player Numbers 0 Answers

Unity networking tutorial? 6 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