Home > other >  how to fix error cs1061 Unity. Not what it seems like, very confusing
how to fix error cs1061 Unity. Not what it seems like, very confusing

Time:09-27

When my prefab gets spawned into the game it forgets the game objects field's fill. if you don't get it (i found it hard to describe), then the following images explain it:

what it looks like when spawned in:

image

what it is supposed to look like:

image

So I have this code that fixed the problem for some different things but won't work on a button.

This script is attached to the button:

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

public class ShootButton : MonoBehaviour
{
public static ShootButton Instance { get; private set; }
void Awake()
{
    if (Instance != null && Instance != this)
    {
        Destroy(this);
    }
    else
    {
        Instance = this;
    }
}
}

And this script is attached to the player:

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

public class Weapon : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public Button shootButton;

void OnEnable()
{
    if (shootButton == null)
    {
        shootButton = ShootButton.Instance;
    }
}

void Start()
{
    shootButton.onClick.AddListener(shootButtonTrue);
}

void shootButtonTrue()
{
    Shoot();
}

void Shoot()
{
    Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}

but when i try that out i get this error:

Assets\stuff\Scripts\Weapon.cs(16,27): error CS0029: Cannot implicitly convert type 'ShootButton' to 'UnityEngine.UI.Button'

If you need any other information or code then please ask. I am a noob at unity i only know a little bit so I wont understand complicated answers Thanks in advance!

CodePudding user response:

idk but try

public Button shootButton;

void OnEnable()
{
    if (shootButton == null)
    {
        shootButton = ShootButton.Instance.GetComponent<Button>();
    }
}

if it is not working try :

public Transform firePoint;
public GameObject bulletPrefab;
public GameObject shoot;



void OnEnable()
{
    if (shootButton == null)
    {
        shoot = ShootButton.Instance;
    }
}
void Start()
{
    shoot.GetComponent<Button>();
    shootButton.onClick.AddListener(shootButtonTrue);
}

and again if it s not working :

public Button shootButton;
public Button shootButtonPrefab;

void OnEnable()
{
    if (shootButton == null)
    {
        Instantiate(shootButtonPrefab, gameObject.transform);
        shootButton = GameObject.Find("the name of the button prefab").GetComponent<Button>();
    }
}
  • Related