Szymon Grzech
Szymon Grzech

Reputation: 9

Unity Engine Vector3 Error

I've finished my code a bit but I was trying all ways to fix error

Vector3 is a 'type' but is used like a 'variable'

Line of Code where that error happen

    private void Update()
    {
        bool keyDown = Input.GetKeyDown(KeyCode.Keypad0);
        if (keyDown)
        {
            Client[] array = UnityEngine.Object.FindObjectsOfType<Client>();
            for (int i = 0; i < array.Length; i++)
            {
                Client client = array[i];
                client.send_createent(Vector3,Vector3,Vector3,Vector3, 10);

            }
        }

(Code for Spawning a entity assembly-csharp.dll code. I'm new to programming so if I talk something too "noob" please understand)

Upvotes: 1

Views: 1374

Answers (1)

Jarak
Jarak

Reputation: 982

It looks like the problem is exactly what the error message is telling you - that you are trying to use a type as a variable. Do you understand the difference between a class and an object? The type is a class, and a variable is an object. So, a variable is an instance of a type.

Think of it this way: The type is like the blueprints for something you wish to create. It lays out the structure of the intended house/object/etc., but it doesn't necessarily define all the specifics. More importantly, the blueprints don't actually give you a house that can be lived in, just guidelines on what one should look like. The variable is a particular instance of that house. You can define specifics that are left vague in the blueprint, and moreover, it gives you a real house that someone could live in.

So what you need to do here is to change the code so that you are making some variables, so that Unity can actually use some concrete variables with relevant information. So it would look a bit like the below (though the numbers used to create the Vectors will vary, depending on your exact application):

private void Update()
{
    bool keyDown = Input.GetKeyDown(KeyCode.Keypad0);
    if (keyDown)
    {
        Client[] array = UnityEngine.Object.FindObjectsOfType<Client>();
        Vector3 vector1 = new Vector3(1.0f, 1.0f, 1.0f);
        Vector3 vector2 = new Vector3(1.0f, 1.0f, 1.0f);
        Vector3 vector3 = new Vector3(1.0f, 1.0f, 1.0f);
        Vector3 vector4 = new Vector3(1.0f, 1.0f, 1.0f);
        for (int i = 0; i < array.Length; i++)
        {
            Client client = array[i];
            client.send_createent(vector1,vector2,vector3,vector4, 10);

        }
    }

Does that help?

EDIT: Thought I should mention that you should take a look at Unity's Vector class (if you look closely, you'll see that it says struct at the top, but for current purposes just ignore that. Classes and structs are different forms of type). Note that there is a constructor listed there. This constructor is what lets you 'instantiate' a Vector object from the Vector class, turning Unity's blueprints into a real vector that the engine can use. The variables listed above that don't have any meaning (and you can't access them) until you have created an object, because they are specific to that object.

Upvotes: 2

Related Questions