Sword Script

In the sword script, we are calling a blank GameObject to be our slash animation spawn point. I am getting an error message from unity that said GameObject’s position cant be translated into the Instantiate command under the Attack method (CS1503 cannot convert from UnityEngine.GameObject to UnityEngine.Vector3). Changing the GameObject to a Vector3 at the top of the script is one workaround, but I wonder if I am missing something?

public class Sword : MonoBehaviour
{

    private PlayerControls playerControls;
    private Animator myAnimator;
    private PlayerController playerController;
    private ActiveWeapon activeWeapon;
    [SerializeField] private GameObject slashAnimPrefab;
    [SerializeField] private GameObject slashAnimSpawnPoint;
    private GameObject slashAnim;

    private void Awake()
    {
        playerController = GetComponentInParent<PlayerController>();
        activeWeapon = GetComponentInParent<ActiveWeapon>();
        playerControls = new PlayerControls();
        myAnimator = GetComponent<Animator>();
    }

    private void OnEnable()
    {
        playerControls.Enable();
    }

    private void Start()
    {
        playerControls.Combat.Attack.started += _ => Attack();
    }

    private void Update()
    {
        MouseFollowWithOffset();
    }

    private void Attack()
    {
        myAnimator.SetTrigger("Attack");
        slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint, Quaternion.identity);
        slashAnim.transform.parent = this.transform.parent;
    }

    private void MouseFollowWithOffset()
    {
        
        Vector3 mousePos = Input.mousePosition;
        Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(playerController.transform.position);

        float angle = Mathf.Atan2(mousePos.x, mousePos.y) * Mathf.Rad2Deg;

        if (mousePos.x < playerScreenPoint.x) 
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0, -180, angle);
        }
        else
        {
            activeWeapon.transform.rotation = Quaternion.Euler(0, 0, angle);

You’re declaring your slash animation spawn point as a GameObject type.

[SerializeField] private GameObject slashAnimSpawnPoint;

and trying to pass it into Instantiate()

slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint, Quaternion.identity);

But the Instantiate () method is expecting either Transform or Vector3 type in that field.
Most likely you want to do slashAnimSpawnPoint.transform instead, though I can’t see what the context is in relation to your course

1 Like

Great reply especially for not having the course content! Thanks for helping out.

Geth270 is correct in the issue and the solution we used was simply to be

[SerializeField] private Transform slashAnimSpawnPoint;

Not

[SerializeField] private GameObject slashAnimSpawnPoint;

Thank you for the responses.

I apologize for creating confusion - the GameObject slashAnimSpawnPoint above was a product of me trying different types of info to troubleshoot. I did not include the original code that caused the error in my post.
The original Transform slashAnimSpawnPoint was what produced the error mentioned in the OP.
I could change it to a Vector3 and call it a day, but I feel like I may be missing something?

1 Like

Found it. Chalk it up to another silly mistake.

I wrote slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint, Quaternion.identity);
not
slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint.position, Quaternion.identity);

Thanks again for the responses!

1 Like

The reason Instantiate() didn’t work when your slashAnimSpawnPoint was Transform type is because you were entering Quaternion.identity as a 3rd parameter and the Instantiate() method doesn’t have an overload for that, it either expects an Object type (or it’s derivative GameObject) followed by type Transform or that followed by a bool.
It’s best if you refer to the Unity doc on Instantiate() to better understand the function and its different overloads.
Depending on what behavior you want there can be a difference between passing in a Transform vs passing in a Vector3 and Quaternion separately.

3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms