- Home /
The more pages the closer he comes.
I'm trying to make for my slender game that every page i collect his teleport radius from player decreases. I know exactly how to make this but how do i access the pageCollect script from my slenderAI script? Here are the both scripts:
slenderAI
using UnityEngine;
using System.Collections;
public class slenderAI : MonoBehaviour {
public Transform player;
public bool isVisible = false;
public AudioClip Static;
void Update()
{
//just gonna cut everything not wanted
if(Distance > 80 && timer == 0 && isVisible == false && pages > 1){
Vector3 position = player.position;
float distance = Random.Range(20.0f,35.0f);
Vector3 objPosition = position - player.forward* distance;
transform.position = objPosition;
}
if(Distance > 70 && timer == 0 && isVisible == false && pages > 2){
Vector3 position = player.position;
float distance = Random.Range(18.0f,33.0f);
Vector3 objPosition = position - player.forward* distance;
transform.position = objPosition;
transform.position = objPosition;
} //and so on
And here is the pageCollect:
using UnityEngine;
using System.Collections;
public class PageCollect : MonoBehaviour {
public int pages = 0;
void Update () {
if(pages == 3)Application.LoadLevel(0);
RaycastHit hit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Input.GetKey(KeyCode.E) || Input.GetMouseButtonDown(0)) {
if (Physics.Raycast(transform.position, fwd, out hit, 2)) {
if (hit.collider.tag == "Finish") {
Debug.Log ("Page grabbed!");
pages +=1;
Destroy(hit.transform.gameObject);
}
else Debug.Log ("Not a page");
}
}
}
Answer by tanoshimi · Nov 02, 2013 at 08:00 AM
Did you try reading the manual? Assuming your scripts are attached to the same gameObject:
OtherScript otherScript = GetComponent(OtherScript);
otherScript.DoSomething();
If they're on different game objects, try reading the following topic: http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
(p.s. this is probably one of the most commonly asked questions on Unity... did you try searching first?)
I tried
void Start(){
GetComponent<PageCollect>().pages = 0;
}
But it gives me:
Object reference not set to an instance of an object
slenderAI.Start () (at Assets/Scripts/slenderAI.cs:27)
Firstly, you should generally cache components returned by GetComponent, espectially if you'll be accessing them frequently:
PageCollect _pageCollect;
void Start(){
_pageCollect = GetComponent<PageCollect>();
_pageCollect.pages = 0;
}
Secondly, what actually is line 27 of SlenderAI.cs that is erroring? Your code is cut so I cannot tell.
Line 27 is: GetComponent().pages = 0 ;
But now it gives me same error on Line 29:
_pageCollect.pages = 0;
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Another way to reference scripts? 2 Answers
Assigning varaibles to prefabs 1 Answer
Declaring Variable in Child & Access as Parent 2 Answers
Change variable value of a script from another script. both in different Game object 1 Answer