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 /
  • Help Room /
avatar image
0
Question by jgvargas · Apr 25, 2017 at 04:47 PM · scripting problemnetworkingcollidermultiplayer

Updating a variable from one script from another or alternative ideas for a solution

I am new to unity and I am hoping to get some feedback on a design issue I've encountered. As I have looked on here the more I believe that my current implementation is flawed and open to alternative views! Thanks in advance.

I am working on a multiplayer game which players are supposed to collect items from a level, the thing is I would like for the items they collect to respawn after some time.

Currently if the player collides with an object the player script despawns the item and I would need to communicate to the item script that there is one less active item.

Player Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
 
 public class Controller_Player : NetworkBehaviour {
 
     // Threshold is used to determine the distance a player falls before respawning
     public float threshold;
 
     // Assigned an empty game opject for spawn point to be assigned
     public Transform playerSpawnPoint = null;
 
     public float powerTimer;
 
     public float speed;
     public float speedMod = 1.0f;
     public float jumpHeight = 100.0f;
     private bool onGround = false;
 
     // Physics component
     public Rigidbody rigidbody_ref;
 
     ScoreScript score;
 
     Camera cam;
     public float distance = 5.0f;
 
     //public Controller_PowerUps pUp = GetComponent<Controller_PowerUps>();
 
    // public override void OnSta()
     //{
         //score = GetComponent<ScoreScript>();
       //  score = GameObject.Find("RoundManager").GetComponent<ScoreScript>();
     //}
 
     public override void OnStartLocalPlayer()
     {
         rigidbody_ref = GetComponent<Rigidbody> ();
         
         score = GameObject.FindGameObjectWithTag("ScoreManager").GetComponent<ScoreScript>();
         //Sets camera target when player is spawned on network
         Camera.main.GetComponent<ThirdPersonCamera>().lookAt = transform;
         cam = GameObject.Find ("Player Camera").GetComponent<Camera>();
 
         powerTimer = Time.time + 10;
         
         //GameObject powerUps = Game
     }
 
     void Start()
     {
 
     }
 
     //Update: Called before a frame is rendered. 
     void Update()
     {
         if (!isLocalPlayer)
             return;
 
         if (Input.GetKeyDown("space") && onGround == true)
         {
             jump ();
         }
 
         // Checks if time for PowerUp has expired
         PowerUpTimer ();
     }
 
     //FixedUpdate: Before any physics is applied
     void FixedUpdate()
     {
         if (!isLocalPlayer)
             return;
 
     // Used to spawn player when falling off the map
         if (transform.position.y < threshold) {
             transform.position = playerSpawnPoint.position;
             rigidbody_ref.velocity = new Vector3(0, 0, 0);
 
             // Also, decrease points of fallen player
             score.SubPoints(1);
             score.SetCountText();
         }
     // Code for player movement
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         //Ribidbody: class, .AddForce to move RB
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         Vector3 actMovement = cam.transform.TransformDirection (movement);
 
         rigidbody_ref.AddForce (actMovement * (speed * speedMod));
 
     }
         
     void OnTriggerEnter( Collider other)
     {
         // Calculates points from ScoreScript
         if (other.gameObject.CompareTag("PickUp"))
         {
             NetworkServer.Destroy(other.gameObject);
 
             score.AddPoints(1);
             score.SetCountText();
         }
 
         if (other.gameObject.CompareTag ("PowerUp")) 
         {
             NetworkServer.Destroy (other.gameObject);
 
             // Cause player to glow, indicates Player has PowerUp
 
             // Check to see which PowerUp was picked up
             //if (other.name == "SpeedUp")
             speedMod = 4;
         }
     }
 
     // OnCollisionStay: Called once per frame for every collider/rigidbody
     //                     that is touching rigidbody/collider
     void OnCollisionStay ()
     {
         onGround = true;
     }
 
     //Calculates force to apply for collision
     void OnCollisionEnter(Collision col)
     {
 
         if (col.gameObject.CompareTag("Player"))
         {
             Rigidbody otherRigidbody = col.collider.GetComponent<Rigidbody>();
             Vector3 test = GetComponent<Rigidbody>().velocity;
 
 
             otherRigidbody.AddRelativeForce(test);
             test = Vector3.Reflect(test, Vector3.right);
         }
 
     }
 
     void jump()
     {
         Vector3 jump = new Vector3 (0.0f, jumpHeight, 0.0f);
         rigidbody_ref.AddForce (jump);
 
         onGround = false;
     }
 
     void PowerUpTimer()
     {
         //Update time
         float timeLeft = powerTimer - Time.time;
 
         //Time expired; reset values
         if (timeLeft < 0) 
         {
             timeLeft = 0.0f;
             speedMod = 1;
         }
 
     }
 }


and item script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class Controller_PowerUps : NetworkBehaviour {
 
     public GameObject powerUpPrefab;
     public int numberOfPowerUps;
     private int PowerUpCount;
     public float spawnWait;
 
     float nextSpawn;
 
     public Transform[] spawner;
 
     Vector3 spawnPosition;
 
     // Use this for initialization
     public override void OnStartServer()
     {
         if (!isServer)
             return;
 
         nextSpawn = Time.time + spawnWait;
 
         for (int i = 0; i < numberOfPowerUps; i++)
         {
             Spawn();
             PowerUpCount++;
         }
     }
 
     // Update is called once per frame
     void Update () {
 
         if (nextSpawn <= Time.time && PowerUpCount < numberOfPowerUps)
         {
             Spawn();
             PowerUpCount++;
             nextSpawn = Time.time + spawnWait;
         }
     }
 
     void Spawn()
     {
         spawnPosition = spawner [PowerUpCount].transform.position;
 
         GameObject pickUp = (GameObject)Instantiate(powerUpPrefab, spawnPosition, new Quaternion(0, 0, 0, 0));
         NetworkServer.Spawn(pickUp);
     }
 }
 


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.

Follow this Question

Answers Answers and Comments

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

HELP with Unity Networking 0 Answers

OnTriggerEnter Issue - Collider problem 0 Answers

How can I send update to all clients from one client in Multiplayer game in Unity3d? 0 Answers

Can i use unity networking to make mmorpg 2 Answers

[Multiplayer Lobby] NetworkManager has a NetworkIdentity component 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