- Home /
How do I assign 3 variables to every GameObject in the array "enemy"?
Hey guys, I really really need help on this. I created a 2-dimensional array that is editable in the inspector. The first array is "Waves," (which allows me to choose how many waves my game has before you win) and the array inside "Waves" is "enemy" (which is a GameObject). Here is what it looks like in the inspector........
I am just simply trying to assign 3 variables to each GameObject that the array "enemy" holds, the variables being...... public int count; public float lowRange; public float highRange; I want to do this so that I can edit these variables to however I choose for each enemy in the inspector. So I can enter the count, lowRange, and highRange for each enemy in the wave.
Here is a picture of the script for the called array "Waves."
I have been trying to make the three declared variables visible in the inspector under each GameObject but it doesn't show up. Please help I have been stuck for awhile now.
Answer by DanVioletSagmiller · Jun 12, 2018 at 06:56 PM
Variables inside methods cannot be public. They have to be at the class level.
I think what you want, is instead of Public GameObject Enemies[]; I think you want to use public EnemyLoadSetup EnemyLoads[];
With a class like this:
[System.Serializable]
public class EnemyLoadSetup // Not a monobehaviour
{
public GameObject[] EnemyType;
public int Count;
public float lowRange;
public float highRange;
}
If I do that though, this is the result
I need the three variables to have distinct values for EACH GameObject, not the same value among ALL of the GameObjects in a wave.
Answer by thepigeonfighter · Jun 12, 2018 at 07:40 PM
So I think what @hpdvs2 was saying is instead of using an array of GameObjects you could have an array of Enemies.
[System.Serializable]
public class Enemy // Not a monobehaviour
{
public GameObject EnemyType;
public int Count;
public float lowRange;
public float highRange;
// Just make a constructor here
public Enemy ( GameObject enemy ,int _count, float _lowRange, float _highRange)
{
EnemyType = enemy;
Count = _count;
lowRange = _lowRange;
highRange = _highRange;
}
}
public class OtherClass
{
public int enemyCount;
public Enemy[] Enemies = new Enemy[enemyCount];
public GameObject enemyPrefab;
void Start()
{
/*Obviously this sets values for all the enemies but it would not be hard to customize for desired effect.
Just set the values in scripts and skip the editor(i think would be easier)
or alternatively you could set up the ints for different kinds of enemies in the
inspector. I.E( weakenemy has certain stats etc)
*/
for(int i = 0 ; i < Enemies.length; i++)
{
Enemies[i] = new Enemy(enemyPrefab,count,lowRange,highRange);
}
}
}
I am confused, do you think you could explain this in a different way? I feel like your answer would work, but I'm having a hard time understanding your method.
You lost me when you declared a public class.
Your answer
Follow this Question
Related Questions
Inspector Doesn't Update Array Elements When Updated In Array Constructor 1 Answer
Why can't I access the Name field in the Custom Styles section of a GUISkin in the Inspector? 2 Answers
Trying to Sort a Multi Dimensional Array 1 Answer
Why does new GUIStyle() give "object reference not set to an instance of an object" error? 1 Answer
Multidimentional Array for Inventory style system in inspector. 1 Answer