Reputation: 29
I want the sprite to make an impulse everytime I click the left button mouse but I'm getting errors from the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Aviao : MonoBehaviour
{
Rigidbody2D fisics;
private void Awake()
{
this.fisics = this.GetComponent<Rigidbody2D>();
}
private void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
this.Impulsionar();
}
}
}
private void Impulsionar()
{
this.fisica.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
}
Assets\Script\Aviao.cs(21,5): error CS8803: Top-level statements must precede namespace and type declarations.
Assets\Script\Aviao.cs(21,5): error CS0106: The modifier 'private' is not valid for this item
Upvotes: -3
Views: 67
Reputation: 755
The error that you get is because your function Impulsionar()
is out of your class and it is recognised as a modifier and not as a function.
Replace your code with the one below, it should fix the errors.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Aviao : MonoBehaviour
{
Rigidbody2D fisics;
private void Awake()
{
this.fisics = this.GetComponent<Rigidbody2D>();
}
private void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
this.Impulsionar();
}
}
private void Impulsionar()
{
this.fisica.AddForce(Vector2.up * 10, ForceMode2D.Impulse);
}
}
Upvotes: 2