Home > Mobile >  Unity giving compile error without any error
Unity giving compile error without any error

Time:05-31

I am learning unity 3d game development and I was following a tutorial and wrote this code for a ball to apply force on its X-axis, but unity said all compile errors must be resolved before going in Playmode:

code:

using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddConstantForce : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        GetComponent<RigidBody>().velocity = new Vector3(2,0,0);
    }
}

I have update unity version. tried various new sample scenes, and after this error pops it also gives the error with default empty void start and void update functions too. please help

CodePudding user response:

Without the error information, this is somewhat difficult to debug but I do see two errors with your code.

First, you have a typo on your Update function you are calling GetComponent<RigidBody> when the actual name of the class is Rigidbody see Unity's documentation for reference https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

There is also an ambiguous reference between Vector3 as both Systems.Numerics & UnityEngine defines that type. you want to stick with the UnityEngine definition. so just delete the first line.

So in the end this code should be working without compilation errors

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddConstantForce : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
    GetComponent<Rigidbody>().velocity = new Vector3(2,0,0);
    }
}

CodePudding user response:

Try define the getcomponet variable in the void update and then make a new line with the varible first then velocity thing or/and use = insted of =.

CodePudding user response:

also dont use first using command

  • Related