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

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小屋 2022-07-14 11:54
    关注

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

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

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包
  • ¥15 comsol压电材料数据
  • ¥35 用python实现除法算法中goldschmidt算法
  • ¥15 汇编代码转换成C代码
  • ¥15 除法算法中的归一化具体是怎么变的?