Error cs1503 unity

using UnityEngine; using System.Collections; public class Tree : MonoBehaviour { public int Health = 5; public Transform logs; public Transform coconut; public GameObject tree; public Camera myC...

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();
        
        }   
    }
}

DonGan

0 / 0 / 0

Регистрация: 10.11.2020

Сообщений: 2

1

10.11.2020, 14:54. Показов 5136. Ответов 1

Метки unity (Все метки)


Я хочу сделать так чтобы создавалась копия объекта. Это у меня получается, но не получается создать эту копию подальше от самого объекта. Вылезает ошибка: error CS1503: Argument 2: cannot convert from ‘UnityEngine.Vector2’ to ‘UnityEngine.Transform.
В C# я плохо разбираюсь поэтому пишу сюда.
Сам код:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class SpawnGuns : MonoBehaviour
{
    public GameObject granateSpawn;
    int check = 0;
 
    void Start()
    {
        check = Random.Range(1, 8);
    }
 
    void Update()
    {
        if (check > )
        {
            GameObject c = Instantiate(granateSpawn, new Vector2(transform.position.x - 0.1f, transform.position.y - 0.3f));
            check = 0;
        }
    }
}

__________________
Помощь в написании контрольных, курсовых и дипломных работ, диссертаций здесь



0



1max1

3088 / 1617 / 921

Регистрация: 26.10.2018

Сообщений: 4,620

10.11.2020, 15:21

2

Лучший ответ Сообщение было отмечено DonGan как решение

Решение

Instantiate не принимает только 2 аргумента.

C#
1
2
var p = new Vector2(transform.position.x - 0.1f, transform.position.y - 0.3f);
Instantiate(granateSpawn, p, Quaternion.identity);



1



error CS1503: Argument `#1′ cannot convert `UnityEngine.Vector3′ expression to type `UnityEngine.Ray’

I don’t even know what’s going wrong here, the Unity database is super vague and I just don’t know what I’m doing by using t$$anonymous$$s, but I’m having a problem with Spherecast. I’ve tried searc$$anonymous$$ng for answers online, but I couldn’t find an answer that solved my problem.

So t$$anonymous$$s is the ONE line of code that has huge problems, I dont know if I need to supply more code, but if I do, I’d happily provide you with more of the code. Anyway, here goes:

I dont know what is going wrong. I dont know why t$$anonymous$$s error is caused, when it seems like wherever I look, it tells me to give a vector3, w$$anonymous$$ch I am doing.

Some clarification or help would be great, thanks guys, Im just utterly lost on t$$anonymous$$s one

3 Ответов

Ответ от Landern · 19/12/16 17:10

You’re missing the RaycastHit.

The rest of the parameters after the RaycastHit have default parameters but you must supply origin, radius, direction, and $$anonymous$$tinfo parameters.

Ответ от UnityCoach · 19/12/16 17:16

According to the documentation, there are three implementations :

public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit $$anonymous$$tInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

public static bool SphereCast(Ray ray, float radius, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

public static bool SphereCast(Ray ray, float radius, out RaycastHit $$anonymous$$tInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

The first one is the only one to take a Vector3. So you need to at least pass it : (Vector3 origin, float radius, Vector3 direction, out RaycastHit $$anonymous$$tInfo)

Источник

Понравилась статья? Поделить с друзьями:
  • Error cs1503 argument 2 cannot convert from double to float
  • Error cs1503 argument 1 cannot convert from void to string
  • Error cs1503 argument 1 cannot convert from unityengine vector3 to float
  • Error cs1503 argument 1 cannot convert from system collections ienumerable to string
  • Error cs1503 argument 1 cannot convert from string to int