Quantcast
Channel: Answers for "Accessing variables from GameObjects in an array"
Viewing all articles
Browse latest Browse all 6

Answer by skovacs1

$
0
0

General problems that could be omissions

You never close your OnGUI.

You may have just omitted from your included code, but there are two immediate problems that I see with this script:

1 What are enemyName, enemy, enemyVector, and namePos within the code sample you've provided? They appear to be undeclared. consider adding the declarations like:

var enemy : GameObject;
var enemyvector : Vector3;
var enemyName : string;
var namePos : Vector2;

2 What is enemyscript within the code sample you've provided? It is never assigned. Your types are a bit confusing. In enemyscript.GetComponent(enemyHUD).enemyName;, you have an uninitialized enemyHUD script (enemyscript) on which you are calling GetComponent to get an attached enemyHUD script? This will always return null even if enemyscript were initialized. Then you try to access its enemyName variable and what do you expect to get? Perhaps you meant:

//get some game object to get the enemyHUD script of.
enemyscript = someGameObject.GetComponent(enemyHUD);
enemyName = enemyscript.enemyName;

Coding practices

Consider naming your variables something clearer. locations is storing GameObjects, but the GameObject's transform.position would actually store the location, not the GameObject itself, especially if it also has a script storing the enemyName.

For simplicity, have you considered using a foreach equivalent?

for (var enemy : GameObject in locations){}

Potential problems

Is the enemyName of the script accessible within this scope?

As names are case sensitive, be sure that everything is spelled and cased correctly. Is enemyHUD the correct name of the script exactly?

Does the enemy in the locations array have an enemyHUD script attached? If not, then the GetComponent is returning a null object and when you try to access the enemyName of this null object, you are trying to access something that doesn't exist which could be problematic. Try checking for null:

var script = enemy.GetComponent(enemyHUD);
if (script == null)
{
    Debug.Log("No enemyHUD script attached!", enemy);
    continue;
}
enemyName = script.enemyName;

To find this kind of crash problem yourself, try breaking your function calls up to isolate the function causing your problem and comment out any unneeded code that you know to be working. Also, use debug and print statement to trace the values (like printing the size of the array and current index to check your array indexing if using an indexing for loop or printing the names or IDs or object of the GameObjects to find which one is in error).

Try something like:

var enemyName : string;
var enemyscript : enemyHUD;
var enemies : GameObject[];

function Start() //initialize enemy array
{
    enemies = GameObject.FindGameObjectsWithTag("pivot");
}

function Update() //enemies won't likely change more than once per frame
{
    enemies = GameObject.FindGameObjectsWithTag("pivot");
}

function OnGUI() // This is run multiple times per frame
{
    for (var enemy : GameObject in enemies)
    {
        enemyscript = enemy.GetComponent(enemyHUD);

        if (enemyscript == null)
        {
            Debug.LogWarning("No enemyHUD script attached!", enemy);
            continue;
        }

        enemyName = enemyscript.enemyName;
    }
}

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>