- Home /
How do I access the contents of a script from another script?
I am a beginner at Unity I am making a 2D game and I have a character and an enemy set up. As of right now my camera follows my character but when I collide with my enemy I want my character to go flying and my camera follow script to stop following my character. How do I do this easily? Thank you in advance.
P.S. Here's the code:
Camera Follow Script:
import System.Collections.Generic;
import UnityEngine;
public var player : GameObject;
static var offset : Vector3;
private var offsetY : Vector2;
function Start ()
{
offset = transform.position - player.transform.position;
}
function LateUpdate ()
{
transform.position = player.transform.position + offset;
}
Enemy Kill Character Script:
import System.Collections.Generic;
import UnityEngine;
public var player : GameObject;
public var script;
function Start()
{
script = GetComponent.<CameraFollow> ();
}
function OnTriggerEnter2D(other : Collider2D)
{
if(other.CompareTag("Player"))
{
player.GetComponent.<Rigidbody2D>().velocity = Vector3(-100,100);
script.enabled = false;
//Application.LoadLevel(1);
}
}
Ignore the LoadLevel part please and my camera script is called CameraFollow and my enemy script is called KillCharacter
Answer by getyour411 · Dec 14, 2016 at 10:23 AM
Not sure what that code is, but to answer your question title
http://answers.unity3d.com/questions/42843/referencing-non-static-variables-from-another-scri.html
Answer by Ali-hatem · Dec 14, 2016 at 11:38 AM
c# : Enemy Kill Character Script:
public bool noFallow;
if(other.CompareTag("Player")){
noFallow = true;
}
Camera Follow Script:
if(Enemy Kill Character Script.noFallow == false){
//fallow the player
}else{
//load the level or do any thing
}
Your answer
Follow this Question
Related Questions
Is constantly referencing or defining once then changing better 1 Answer
How Do I Access and Change Items in a List on Another Script? 2 Answers
How can i access all the objects with whom my script is attached to ??? 1 Answer
Is there a way of seeing which script accessing each other? 1 Answer
Can anyone tell how can i add 2 seconds to my TIMER from another script ? 2 Answers