- Home /
problem with selecting characters script
so i am making a 3rd person game with multiple characters. i am trying to create a script that will select characters and put them in the scene. i also have a script for cameras in the scene to follow the selected characters. this is that script:
using UnityEngine;
using System.Collections;
public class camFollow : MonoBehaviour {
public LayerMask mask;
public float height;
public float distance;
public float x;
public float y;
public float yMin;
public float yMax;
int i = 0;
void Update () {
x += 3 * Input.GetAxis ("Mouse X");
y += 3 * -Input.GetAxis ("Mouse Y");
if(y > yMax){
y = yMax;
}
if(y < yMin){
y = yMin;
}
Quaternion rotation = Quaternion.Euler ( y,x, 0);
Vector3 pos = rotation * new Vector3(0, height, -distance) + cameras.players[i].position;
transform.rotation = rotation;
transform.position = pos;
transform.LookAt (cameras.players[i]);
RaycastHit hit;
if(Physics.Linecast(cameras.players[i].position, transform.position, out hit, mask)){
float tempDistance = Vector3.Distance(cameras.players[i].position, hit.point);
pos = rotation * new Vector3(0, height, -tempDistance) + cameras.players[i].position;
transform.position = pos;
}
}
}
cameras is another script that has all the cameras in it and has this line: static public Transform players[]; and i'm having problems defining players because its a transform not a gameobject but it has to be a transform for the first script to work. any ideas?
Do you mean like defining players in real time ?
public static Transform[] players;
// Use this for initialization
void Start () {
players = new Transform[3];
players[0] = GameObject.Find("Player1").transform; // this is slower if you have a lot of game object
players[1] = GameObject.FindGameObjectWithTag("Player2").transform; // pretty fast
players[2] = transform.Find("Player3"); // more fast, assume you have child Player3 on this script
print(players[2].localPosition);
}
i got this error: NullReferenceException: Object reference not set to an instance of an object
I don't really understand, i don't know your full code, what method do you use in the example above? how do you name it, do you use tag or not? if you use transform.Find("Player3") dont forget to drag your Player3 to a game object where the example script above attached (for instances cameras), so Player3 became a child of cameras.
no i just tried the first players[0] = Gameobject.Find("Player1").transform;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Best way to keep track of objects on a 3D Grid? 2 Answers
Flip over an object (smooth transition) 3 Answers
How to debug values in jagged arrays? 0 Answers