I have a “Temperature” script attached to each player object. Each player also has the tag “Player”. In the scene, I have a object with the following script attached.
public class Fire : MonoBehaviour {
public Temperature ph;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject[] players = GameObject.FindGameObjectsWithTag ("Player");
for (int i=1; i < players.Length; i++) {
if (Vector3.Distance (players*.transform.position, transform.position) < 10) {*
Debug.Log ("Player Near Fire");*
_ GameObject go = players*;_
_ ph = (Temperature)_
_ go.GetComponent (typeof(Temperature));_
_ ph.ChangeTemp(2);_
_ }_
_ }_
_ }_
_}_ Here is the “ChangeTemp” function
_ public void ChangeTemp(int temper) {_
_ temp += temper;_
_ }*_ This should be working, but it prints nothing to the log, nothing happens period. Thanks for any help!
Your code looks fine to me, makes sure you saved the label unto the players. This seems to be the most likely failure. Also make sure you didn’t type a space in the tag anywhere, the tags need to be exact.
I just edited your script to see why it didn’t fired the if statement. I edited it a bit and it should work now.
using UnityEngine;
using System.Collections;
public class Fire : MonoBehaviour {
GameObject[] players;
public Temperature ph;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
players = GameObject.FindGameObjectsWithTag ("Player");
for (int i=0; i < players.Length; i++) {
float dist =Vector3.Distance (players*.transform.position, transform.position);*
_*What I did was setting Vector3.Distance to a float called dist. And let it print each value between object 1 and its own each frame. Then if dist was smaller than 10 it would print “player near fire”. http://docs.unity3d.com/ScriptReference/Vector3.Distance.html*_
Maybe in multiplayer it will be a problem, I tested my answer in Unity with some primitives and it worked just fine. Apart of the Temp which was continuously rising with a value of 2 so the player got hot really quickly :D
What is the value of
– tanoshimiplayers.Length?it is the table declared above it, using the function "FindGameObjectsWithTag"
– ArgoEclipse