- Home /
Setting 2 variables from different classes equal to each other.
So I'm trying to make 2 variables from 2 different classes equal each other. This is from the ArrowScript Class in the void Update() method:
ArcheryTowerScript at = new ArcheryTowerScript();
selectedTarget1 = at.recentEnemy();
So what i have above is declaring a object called at as a ArcheryTowerScript object and then creating it as such. I then use that object to call the recent enemy method within that class. This is the method within the ArcheryTowerScript Class:
public Transform recentEnemy()
{
value1 = 1;
return EnemyArray[0];
}
That was the recentEnemy method and once its called it suppose to return the first element in the array. Im trying to set the selectedTarget equal to the first element in the array. The array is a Transform array that when ever a new object enters the towers radius it gets added to the array. The first element in the array or the object thats been in the radius of the tower the longest gets sent to the ArrowScript class, which lets the selectedTarget1 variable have something to be equal to. Giving it a target to attack. But the problem is that the method call isnt working and im not sure what the problem is. They are also attached to 2 separate objects.
Answer by Robomaster · Oct 25, 2014 at 02:56 AM
ArcheryTower = GameObject.Find("ArcheryTower");
ArcheryTowerScript at = new ArcheryTowerScript();
at = ArcheryTower.GetComponent<ArcheryTowerScript>();
selectedTarget1 = at.recentEnemy();
Looks like i had to use the object it was a attached to properly call the method.