Can someone Help!

Can someone Help me with my code?

It says that

Assets/Scripts/EnemySpawner.cs(39,34): error CS0428: Cannot convert method group NextFreePosition' to non-delegate typeUnityEngine.Transform’. Consider using parentheses to invoke the method.

And here is my code:

using UnityEngine;

public class EnemySpawner : MonoBehaviour
{

    public GameObject enemyPrefab;
    public float width = 12.22f;
    public float height = 9.65f;
    public float speed = 5f;
    public float spawnDelay = 0.5f;

    private bool moveingRight = true;
    private float xmax;
    private float xmin;
    // Use this for initialization


    void Start()
    {
        float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
        Vector3 rightBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
        xmax = rightBoundary.x;
        xmin = leftBoundary.x;
        SpawnUntilFull();


    }

    void SpawnEnemies()
    {
        foreach (Transform child in transform)
        {
            GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
            enemy.transform.parent = child;
        }
    }
    void SpawnUntilFull()
    {
        Transform freePosition = NextFreePosition;
        if (freePosition)
        {
            GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
            enemy.transform.parent = freePosition;
        }
        if (NextFreePosition())  
        {
         Invoke("SpawnUntilFull", spawnDelay);
        }
    }


    public void OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
    }
    void Update()
    {
        if (moveingRight)
        {
            transform.position += Vector3.right * speed * Time.deltaTime;
        }
        else
        {
            transform.position += Vector3.left * speed * Time.deltaTime;
        }
        float rightEdgeOfFormation = transform.position.x + (0.5f * width);
        float leftEdgeOfFormation = transform.position.x - (0.5f * width);
        if (leftEdgeOfFormation < xmin)
        {
            moveingRight = true;
        }
        else if (rightEdgeOfFormation > xmax)
        {
            moveingRight = false;
        }
        if (AllMembersDead())
        {
            Debug.Log("Empty formation");
            SpawnUntilFull();
        }
    }
    Transform NextFreePosition()
    {
        foreach (Transform childPositionGameObject in transform)
        {
            if (childPositionGameObject.childCount == 0)
            {
                return childPositionGameObject;
            }
        }
        return null;
    }
    bool AllMembersDead()
    {
        foreach (Transform childPositionGameObject in transform)
        {
            if (childPositionGameObject.childCount > 0)
            {
                return false;
            }
        }
        return true;
    }
}

Thanks to all thoese who will thake off their time for THIS :)))))

Hi,

You’re calling a method (first line within SpawnUntilFull) but you’ve not added the brackets after its name :slight_smile:

NextFreePosition()

Hope this helps.


See also;

Hello

NextFreePosition returns a transform.

Where you have if(NextFreePosition()), the if statement is expecting a result of true or false.

So you could maybe try if(NextFreePosition() != null) instead.

*edit… Haha, looks like Rob beat me to it, and is probably correct…
The error message points to the line in code with the problem, not having those line numbers can make it a bit tricky to find :slight_smile:

1 Like

It’s a badly written method (from the course), but that will actually work as is. If it returns null it will be treated as false. If it returns a transform, it will be treated as true. :slight_smile:

Ah, ok. I’ve always been a little unsure about that.

1 Like

Give it a little go, try a few different methods with different return types and output to the console etc.

I’m not, personally, overly keen on the assumption with that way of using booleans, e.g. that method had the job of return a transform, yet we want to obtain a boolean - seems a bit messy, but, it works, and it saves writing a few lines of extra code…

I don’t like the two exit points in the method - but that’s another discussion :slight_smile:

Tnx Rob.

It did helpt me out. :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms