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);
}
}
Your answer
Follow this Question
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