【发布时间】: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