weixin_62955149 2022-07-14 11:30 采纳率: 91.7%
浏览 96
已结题

unity无法为对象赋值,如何解决?

请问为什么不能在prefabs里为redBird,yellowBird和GreenBird赋值呢,检查了代码没发现什么问题呀

img


```c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour
{
    private bool isClick = false; 
    public float maxDis = 3;
    [HideInInspector]
    public SpringJoint2D sp;
    protected Rigidbody2D rg;

    public LineRenderer right;
    public Transform right_woodPos;
    public LineRenderer left;
    public Transform left_woodPos;

    public GameObject boom;

    private TestMyTrail myTrail;

    private bool canMove = true;
    public float smooth = 3;

    public AudioClip select;
    public AudioClip fly;

    private bool isFly = false;

    public Sprite hurt;
    private SpriteRenderer render;

    private void Awake()
    {
        sp = GetComponent<SpringJoint2D>();
        rg = GetComponent<Rigidbody2D>();
        myTrail = GetComponent<TestMyTrail>();
        render = GetComponent<SpriteRenderer>();
    }

    private void OnMouseDown()//鼠标按下
    {
        if(canMove)
        {
            isClick = true;
            rg.isKinematic = true;
        }
    }

    private void OnMouseUp()//鼠标抬起
    {
        if(canMove)
        {
            AudioPlay(select);
            isClick = false;
            rg.isKinematic = false;
            Invoke("Fly", 0.1f);
            //禁用划线组件
            right.enabled = false;
            left.enabled = false;
            canMove = false;
        }
    }


    private void Update()
    {
        if (isClick)
        {//鼠标一直按下,进行位置的跟随
            transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            //transform.position += new Vector3(0, 0, 10);//方法1
            transform.position+=new Vector3(0,0,-Camera.main.transform.position.z);//方法2
            
            
            if(Vector3.Distance(transform.position,right_woodPos.position)>maxDis)
            {//进行位置限定
                Vector3 pos = (transform.position - right_woodPos.position).normalized;//单位化向量
                pos *= maxDis;//最大长度的向量
                transform.position = pos + right_woodPos.position;
            }
            Line();
        }


        //相机跟随
        float posX = transform.position.x;
        Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, new Vector3(Mathf.Clamp(posX,0,15),Camera.main.transform.position.y, Camera.main.transform.position.z),smooth*Time.deltaTime);
        if(isFly)
        {
            if(Input.GetMouseButtonDown(0))
            {
                ShowSkill();
            }
        }
    }


    void Fly()
    {
        isFly = true;
        AudioPlay(fly);
        myTrail.StartTrails();
        sp.enabled = false;
        Invoke("Next", 5);
    }

    /// <summary>
    /// 划线
    /// </summary>
    void Line()
    {
        right.enabled = true;
        left.enabled = true;

        right.SetPosition(0, right_woodPos.position);
        right.SetPosition(1, transform.position);

        left.SetPosition(0, left_woodPos.position);
        left.SetPosition(1, transform.position);
    }
    
    /// <summary>
    /// 下一只小鸟的飞出
    /// </summary>
    void Next()
    {
        GameManager._instance.birds.Remove(this);
        Destroy(gameObject);
        Instantiate(boom, transform.position, Quaternion.identity);
        GameManager._instance.NextBird();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        isFly = false;
        myTrail.ClearTrails();
    }

    public void AudioPlay(AudioClip clip)
    {
        AudioSource.PlayClipAtPoint(clip, transform.position);
    }


  • 写回答

2条回答 默认 最新

    报告相同问题?

    相关推荐 更多相似问题

    问题事件

    • 系统已结题 7月23日
    • 已采纳回答 7月15日
    • 创建了问题 7月14日

    悬赏问题

    • ¥15 gradle项目报错
    • ¥60 有关C#的问题,基础牢固的来
    • ¥15 Python算法设计Seam carving--让图片比例随心缩放
    • ¥15 页面制作,HTML CSS
    • ¥15 通个批处理实现 win7和win10 修改或设置:显示操作系统列表时间为2秒
    • ¥20 报错:Unsupported class file major version 59
    • ¥15 用ASP代码实现跨站脚本攻击
    • ¥50 ABP中怎样接收vue上传的图片
    • ¥15 uniapp设置音频的倍速播放无效
    • ¥15 关于#hc#的问题,如何解决?