- Home /
Access variable from script on other gameobject.
I'm trying to access a variable from another gameobject to reference it but i'm not having any luck since i only know how to do it if the components are all on the same object.
Here's a simplified version of the 2 scripts without the extra functions.
Enemy_Attacked.js on Enemy
var MeleeInstance;
function Start () {
Meleeinstance = GetComponent(Melee);
}
function Update () {
}
function OnTriggerEnter (col : Collider)
{
if(MeleeInstance == true)
if(col.tag == "Weapon")
{
MeleeInstance.swordReady = false;
}
}
Melee.js On player Code:
public var Enemy_Attacked_Instance;
var swordReady : boolean = false;
function Start () {
Enemy_Attacked_Instance = Enemy_Attacked;
}
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
swordReady = true;
StartCoroutine("DisableSwordReady");
}
}
function DisableSwordReady()
{
yield WaitForSeconds(1);
swordReady = false;
}
Answer by getyour411 · Feb 02, 2014 at 09:32 PM
Try
MeleeInstance = GameObject.FindObjectWithTag("Player").GetComponent.<Melee>();
or whatever the JS equiv is
This site has great tuts on getting components from script a / script b http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
Thanks for the reply! I'll review this info asap and get back to you. I've seen that code before and it does seem like its the way to go. (I've read the listed tutorial and its what I have working for the rest of the scripts that's not posted but doesn't work with calling a component from another object :(
(actually i may have a fix. i'll apply it in the morning and see what happens. Think I just need to do abit of switching around)
Hm, i'm getting:
Assets/Scripts/Enemy_Attacked.js(7,69): BCE0138: 'quack' is not a generic definition.
Seems this guy had the same problem, but i'm close. Just have to restructure this. hm
http://forum.unity3d.com/threads/87870-Static-vars-and-multiple-enemies
Well i got halfway into fixing this last part but in melee.js I can't change swordready to either true or false with a swordReady = true/false. On the other hand, in Enemy_Attacked I can do melee.swordReady = true/false and it will change so idk.
Edit Been working on this for about 15 hours straight lol. Will continue after I catch some sleep.
Example of current code. $$anonymous$$elee.js attached to player
var myEnemyObject : GameObject;
var swordReady : boolean = false;
function Start () {
myEnemyObject = GameObject.Find("Enemy_Attacked");;
}
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
swordReady = true;
networkView.RPC("SyncAttackAnimation", RPC$$anonymous$$ode.All);
StartCoroutine("DisableSwordReady");
}
}
function DisableSwordReady()
{
yield WaitForSeconds(1);
swordReady = false;
}
@RPC
function SyncAttackAnimation(){
animation.Play("Attack");
animation["Attack"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
Enemy_Attacked attached to enemy
var enemyLogicInstance;
var myPlayerObject : GameObject;
function Start () {
enemyLogicInstance = GetComponent(EnemyLogic);
myPlayerObject = GameObject.Find("$$anonymous$$elee");
}
function Update () {
}
function OnTriggerEnter (col : Collider)
{
var melee = myPlayerObject.GetComponent($$anonymous$$elee);
if(col.tag == "Weapon")
{
if(melee.swordReady == true)
{
melee.swordReady = false;
networkView.RPC("RemoveHealth", RPC$$anonymous$$ode.All);
}
}
}
@RPC
function RemoveHealth(){
enemyLogicInstance.Enemy_Health -= 25;
print(enemyLogicInstance.Enemy_Health);
}
Your answer
Follow this Question
Related Questions
How do I assign a prefab reference to a private variable? 1 Answer
Setting a game object from a list to be the active character that gets instantiated 1 Answer
What is the type of my prefab? 2 Answers
Set Rigidbody variable to Prefab using code 1 Answer
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers