First of all, when you have 1 compile time error in your code, you stop and fix it before writing more code. You can’t just create errors on top of other errors in your code.
There are more than 13 errors in your code but only 2 mistakes created those errors.
1.You must use a new
keyword to create a new vector. One exception is when you are calling static
functions or constructors such as Vector3.zero
,Vector3.up
and so on.
So replace Vector3(Random.Range...
with new Vector3(Random.Range.....
And tree.transform.position + Vector3(0, 0, 0)
with tree.transform.position + new Vector3(0, 0, 0)
Do this for 5 other mistakes too.
2.Vector3
takes float
,float
,float
as a parameter not int
,int
,int
.
In this line of Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0))
;, you are passing in int
to Vector3
instead of float
. To fix this, you put f after each value in the Random.Range
function. The f lets the compiler know that this is a float
not int
. If you don’t. Random.Range int overload method will be called instead of float function overload.
So again change Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
to new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));
.
public class Tree : MonoBehaviour
{
public int Health = 5;
public Transform logs;
public Transform coconut;
public GameObject tree;
public Camera myCamera;
public int speed = 8;
void Start()
{
tree = this.gameObject;
GetComponent<Rigidbody>().isKinematic = true;
myCamera = GameObject.FindObjectOfType<Camera>();
}
// Update is called once per frame
void Update()
{
if (Health > 0)
{
if (Vector3.Distance(transform.position, myCamera.transform.root.transform.position) < 10f)
{
if (Input.GetKeyDown(KeyCode.R) && WeaponSwitching.check == true)
{
Ray ray = new Ray(myCamera.transform.position, myCamera.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 10f))
{
if (hit.collider.gameObject == gameObject)
{
--Health;
}
}
}
}
}
if (Health <= 0)
{
GetComponent<Rigidbody>().isKinematic = false;
GetComponent<Rigidbody>().AddForce(transform.forward * speed);
DestroyTree();
}
}
void DestroyTree()
{
wait();
Destroy(tree);
Vector3 position = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));
Instantiate(logs, tree.transform.position + new Vector3(0, 0, 0) + position, Quaternion.identity);
Instantiate(logs, tree.transform.position + new Vector3(2, 2, 0) + position, Quaternion.identity);
Instantiate(logs, tree.transform.position + new Vector3(5, 5, 0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + new Vector3(0, 0, 0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + new Vector3(2, 2, 0) + position, Quaternion.identity);
Instantiate(coconut, tree.transform.position + new Vector3(5, 5, 0) + position, Quaternion.identity);
}
IEnumerator wait()
{
yield return new WaitForSeconds(7.0f);
}
}
using System;
using UnityEngine;
using UnityEngine.InputSystem;
namespace platformer
{
public class HeroInput : MonoBehaviour
{
[SerializeField] private Hero _hero;
private HeroInputAction _inputActions;
private void Awake()
{
_inputActions = new HeroInputAction();
_inputActions.Hero.HorizontalMovement.performed += OnHorizontalMovement;
_inputActions.Hero.HorizontalMovement.canceled += OnHorizontalMovement;
_inputActions.Hero.SaySomething.performed += OnSaySomething;
}
private void OnEnable()
{
_inputActions.Enable();
}
private void OnHorizontalMovement(InputAction.CallbackContext context )
{
var direction = context.ReadValue<float>();
_hero.SetDirection(direction);
}
private void OnSaySomething(InputAction.CallbackContext context)
{
_hero.SaySomething();
}
}
}
<pre>using UnityEngine; using System.Collections; public class Movimento : MonoBehaviour { public Animator Animacao; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Animacao = gameObject.GetComponent<Animator> (); if (Input.GetAxisRaw ("A") == 1) { Animacao.SetBool ("Andar(a)", true); gameObject.transform.Translate(new Vector2(-0.1, 0f)); } if (Input.GetAxisRaw ("A") == 0) { Animacao.SetBool ("Andar(a)", false); } } }
Hi, can you help me with my error? Its in Unity, and the app to i program its VisualStudio (VS Code).
The full error is: AssetsMovimento.cs(18,57): error CS1503: Argument 1: cannot convert from ‘double’ to ‘float’
Help-me pls!!!
What I have tried:
I searched for videos on youtube and searched on google but none helped me, I also tried to change the code but it was not
Get the Reddit app
Scan this QR code to download the app now
Or check it out in the app stores
$begingroup$
I keep getting this error:
AssetsScriptsstore.cs(15,26): error CS1503: Argument 1: cannot convert from ‘string’ to ‘char’
When I try this:
public static string[] toArray(string str) {
return str.Split(" , ");
}
I know other questions have answered this but they haven’t worked.
Why is this happening and how can I fix it?
Thank you.
asked Jun 22, 2021 at 3:30
$endgroup$
$begingroup$
The method require a char
not string
public static string[] toArray(string str) {
return str.Split(',');
}
Try use code editor recommended by Unity like Visual Studio. It show error right away and how to fix them.
answered Jun 22, 2021 at 6:52
$endgroup$
1
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.