Question by
William4458 · Jan 14, 2017 at 11:58 AM ·
prefabvector3variableaccessfindobjectoftype
Accessing variables from prefabs? C Sharp
Hello there! I am currently making a survival game. I made a script that would spawn trees randomly (with raycasts) across the map. And I attempted to make a script on that would chop down trees:
using UnityEngine;
using System.Collections;
public class ChoppingTrees : MonoBehaviour {
public int Chopcounter;
float Distance;
public Transform PlayerPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Distance = Vector3.Distance (FindObjectOfType<PlayerControlsMovement> ().PlayerPosition, transform.position);
if (Chopcounter == 5 && Distance < 3) {
Debug.Log ("You have chopped a tree!");
}
}
void OnMouseDown (){
Chopcounter++;
}
}
(Btw. PlayerPos is just a Vector3 that stores the players position and updates it every frame) Now, this script does kind of work, although it makes my fps drop to about 2. Anyone know how I should go about doing this? Thanks in advance!
Comment
Answer by William4458 · Jan 14, 2017 at 12:13 PM
I kind of fixed the problem by putting " Distance = Vector3.Distance (FindObjectOfType ().PlayerPosition, transform.position);" under the OnMouseDown function but I still would like to know if there would be another solution.