Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to programming and after watching youtube or searching online I can't run a program, because it says error CS1520: Method must have a return type.

C#
    using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class RandomPatrol : MonoBehaviour
{
    public float minX;
    public float maxX;
    public float minY;
    public float maxY;

    Vector2 targetPosition;

    public float speed;

    void Start()
    {
        targetPosition = GetRandomPosition();
    }

    void Update()  
    {
        if ((Vector2)transform.position != targetPosition)
        {
            transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
        }
        else
        {
            targetPosition = GetRandomPosition();
        }
    }
    Vector2 GetRandomPosition()
    {
        float randomX = Random.Range(minX, maxX);
        float randomY = Random.Range(minY, maxY);
        return new Vector2(randomX, randomY);
    }
    
}


What I have tried:

I tried putting void in front, but then it shows even more errors.
Posted
Updated 27-Nov-21 10:26am
v3

In order to return a value - any value - from a method, you have to include the return type in the method signature: void indicates it returns no value, and a type indicates the class or struct that it will return an instance of: Methods - C# Programming Guide | Microsoft Docs[^]

Without a type prefix, it is a constructor, and they cannot return anything as they always return the instance that was created.

You can't "call" a constructor from a constructor, that way just leads to madness and crashing apps when they run out of stack space ...

What you want is probably more like this:
C#
GetRandomPosition(float x = float.MinValue, float y = float.MinValue)
{
    x = x != float.MinValue? x : random.Range(minX, maxX);
    y = y != float.MinValue? y : random.Range(minY, maxY);
    ...
}
Which used optional parameters to do what you wanted.
 
Share this answer
 
Comments
#realJSOP 27-Nov-21 15:38pm    
Actually, you *can* call a constructor from a constructor:

public MyClass(int x, int y)
{
    // do something interesting with x
}

public MyClass(int x) : this(x, 0)
{    
    // do something less interesting with x
}


It comes in handy when you want to make init code similar regardless of how the object was constructed.

Of course, you're probably already aware of that. :)
OriginalGriff 27-Nov-21 15:46pm    
Yep, but I don't think the OP is quite ready for it yet ... :laugh:
Optional parameters are a bit easier to explain in a little text box!
Gnom Ed 27-Nov-21 15:51pm    
Everything is very confusing and nothing is working :(
#realJSOP 27-Nov-21 15:53pm    
Update your question with all of the source code in question.
Gnom Ed 27-Nov-21 15:55pm    
When I try using your example it says "Assets\RandomPatrol.cs(31,13): error CS0246: The type or namespace name 'Position' could not be found (are you missing a using directive or an assembly reference?)
"
Your GetRandomPosition() method needs a return type. Since you're returning the value returned from an overloaded version of the method, you have to return the same type, something like this:
C#
// This would be the method you're calling from the method in question
private Position GetRandomPosition(float x, float y)
{
   return new Position(x, y);
}

// This is the method you're asking about - I added the Position type to  
// the method prototype. I'm only making a guess as to the type name.
private Position GetRandomPosition()
{
    float randomX = random.Range(minX, maxX);
    float randomY = random.Range(minY, maxY);
    // note the change here too. you can't "new" a method call like you 
    //were doing.
    return this.GetRandomPosition(randomX, randomY);
}
 
Share this answer
 
v6
Comments
OriginalGriff 27-Nov-21 15:32pm    
It's a constructor, not a method given the new at the end.
#realJSOP 27-Nov-21 15:42pm    
He didn't say it was a constructor, and given the name, I assumed it was a method. If his object is indeed called GetRandomPosition, he really needs to seriously reconsider about his naming technique...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900