【问题标题】:Can not add script behavior VisualContainerAsset无法添加脚本行为 VisualContainerAsset
【发布时间】:2019-04-28 03:32:07
【问题描述】:

Inspector (for ball sprite)Trying to add the Ball script to "Ball"我正在创建一个 BlockBreaker 游戏 2D,每当我尝试将脚本连接到精灵时,它都会给我错误:“无法添加脚本行为 VisualContainerAsset。脚本需要从 MonoBehaviour 派生。 " 我总共有 6 个脚本,当我尝试将一个脚本附加到一个精灵时,它们每个都给我同样的错误。有人能告诉我为什么吗? SCREENSHOT OF ERROR 这是球脚本:

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

public class Ball : MonoBehaviour {

public Paddle paddle;

bool hasStarted = false;


Vector3 paddleToBallVector;


void Start () {
    paddleToBallVector = this.transform.position - paddle.transform.position; 

}

// Update is called once per frame
void Update () {

    if(hasStarted == false)
    {
        this.transform.position = paddle.transform.position + paddleToBallVector;

        if (Input.GetMouseButtonDown(0)) 
        {
            hasStarted = true;

            this.GetComponent<Rigidbody2D>().velocity = new 
 Vector2(Random.Range(-1f, 3f), 10f); //This is for the ball to bounce randomly
        }
    }



}

void OnCollisionEnter2D(Collision2D myColl)
{
    if (myColl.gameObject.name == "Rightborder" || myColl.gameObject.name == "Leftborder") //If the ball hits either the left border or the right border
    {
        this.GetComponent<Rigidbody2D>().velocity += new Vector2(0f, 1f); //When the ball hits one of the borders, the speed will not be reduced
    }
}

}

【问题讨论】:

  • 我很抱歉我误按了下一个。代码已插入
  • 您是否创建了脚本“VisualContainerAsset”
  • 不,我没有创建那个脚本
  • 你的所有脚本都继承 MonoBehaviour,比如“public class Ball : MonoBehaviour”
  • 是的,他们都有 MonoBehaviour

标签: unity3d


【解决方案1】:

文件名和类名要一致。 IE。如果这个类叫Ball,文件应该叫Ball.csBall.script,我忘了它们是什么保存的。

如果这不起作用,我在统一论坛上找到了这个:

显然您需要重置脚本?

【讨论】:

  • 非常感谢!我终于让他们工作了!谢谢!
  • 任何时候都是我的荣幸!
猜你喜欢
  • 1970-01-01
  • 2019-03-10
  • 2015-05-29
  • 2022-11-23
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 2021-04-27
相关资源
最近更新 更多