- Home /
numerous errors with super basic door transform
Hello,
I'm a super beginner and have a very basic maze game. Your player character is controlled by a script known as "tankMovement". This also contains a public int variable called "count" which counts the tank's number of pickups collected.
On a separate script, "doorOpen", which is attached to a parent group consisting of a door and the wall trigger associated with it, I am trying to have an If statement check the value of tankMovement's "count" integer, and then raise the door up once it hits 5. The "doorOpen" script is below.
using UnityEngine;
using System.Collections;
public class doorOpen : MonoBehaviour {
private tankMovement tankScore;
void Start () {
tankScore = GetComponent<tankMovement> ();
}
void Update () {
if (tankScore.GetComponent<tankMovement>().count==5) {
transform.Translate(new Vector3(0, 1, 0));
}
}
}
I have been getting numerous errors with this all day and it's hurting my brain trying to fix it. Sorry for being such a beginner but I'd love some advice on how to proceed so I can get on with my learning.
Ehh? I would include your Numerous Errors, most find them helpful to isolate where the issue is actually occurring. Syntactically your script is fine.
Answer by Jessespike · Aug 11, 2016 at 06:33 PM
tankMovement and doorOpen are two separate scripts on two separate GameObjects, so GetComponent won't work, that only works if the scripts are on the same GameObject. Make a public reference for the player or tankMovement in the door script. Be sure to drag the player into the reference from the inspector.
public class doorOpen : MonoBehaviour {
public tankMovement tankScore;
private Vector3 openPosition;
void Start () {
openPosition = transform.localPosition + new Vector3(0f, 1f, 0f);
}
void Update () {
if (tankScore.count==5) {
transform.localPosition = openPosition;
}
}
}
Landern also makes a point, be sure to include the errors when asking a question.