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

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条回答 默认 最新

  • 周周的Unity小屋 Unity3D领域优质创作者 2022-07-14 11:54
    关注

    是要给right、left这些对象赋值?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。