简体   繁体   English

如何解决不存在的错误转换?

[英]How can I fix the errors transform not exist?

using UnityEngine;
using System.Collections;

public class MakeTwoPoints3D : MonoBehaviour {

    public GameObject cylinderPrefab;

    // Use this for initialization
    void Start () {

        CreateCylinderBetweenPoints(Vector3.zero, new Vector3(10, 10, 10), 0.5f);

    }

    void CreateCylinderBetweenPoints(Vector3 start, Vector3 end, float width)
    {
        var offset = end - start;
        var scale = new Vector3(width, offset.magnitude / 2.0f, width);
        var position = start + (offset / 2.0f);


        Object cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity);
        cylinder.transform.up = offset;
        cylinder.transform.localScale = scale;
    }

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

    }
}

On both lines same error: 在两行上出现相同的错误:

cylinder.transform.up = offset;
cylinder.transform.localScale = scale;

Severity Code Description Project File Line Suppression State Error CS1061 'Object' does not contain a definition for 'transform' and no extension method 'transform' accepting a first argument of type 'Object' could be found (are you missing a using directive or an assembly reference?) MakeTwoPoints3D.cs 23 Active 严重性代码说明项目文件行抑制状态错误CS1061“对象”不包含“变换”的定义,并且找不到找到接受类型为“对象”的第一个参数的扩展方法“变换”(您是否缺少using指令或程序集引用?)MakeTwoPoints3D.cs 23有效

Object is parent class of GameObject and GameObject as member of type Transform . ObjectGameObject父类,并且GameObjectTransform类型的成员。 If you try to access transform from an instance of Object class, it will show you following error: 如果尝试从Object类的实例访问transform ,它将显示以下错误:

Object' does not contain a definition for 'transform' 对象”不包含“变换”的定义

So accurate way of instantiating and using the resulted object as a GameObject is as Quantic said in comment : 如此精确的实例化和将结果对象用作GameObject的方法,正如Quantic在评论中所说:

GameObject cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity) as GameObject;

OR 要么

GameObject cylinder = (GameObject) Instantiate(cylinderPrefab, position, Quaternion.identity);

It is always to use null-check for safety before using the component in case of other types than GameObject. 对于非GameObject的其他类型,在使用组件之前始终应使用null检查以确保安全。 For example: 例如:

Rigidbody rb = Instantiate(somePrefab) as Rigidbody;
if(rb != null)
  // use it here

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中修复这些错误? - How can I fix these errors in C#? 如何解决当前内容中不存在的“ myLogin”? - How can i fix the 'myLogin' does not exist in the current content? 如何修复我的代码以将已存在的文件移动到另一个目录? - How can I fix my code to move files that already exist to another directory? 如何解决自定义错误? - How do I fix the custom errors? 如何解决这些C#错误? - How do I fix these C# errors? Xamarin更新后如何修复NewtonSoft(json.net)错误? - How can I fix NewtonSoft (json.net) errors after Xamarin update? 如何修复“必须先修复所有编译器错误才能进入播放模式!” Unity中的错误 - How do I fix "All compiler errors have to be fixed before you can enter playmode!" error in Unity 我试图使用SolidColorBrush但得到很多无法转换类型错误如何解决它并使用SolidColorBrush? - Im trying to use SolidColorBrush but getting many Cannot convert type errors how can i fix it and use the SolidColorBrush? 我在Application.loadedLevelName上收到一条警告消息以使用SceneManager,但SceneManager不存在。 我该如何解决? - I'm getting a warning message on Application.loadedLevelName to use SceneManager but SceneManager is not exist. How can i fix it? 如何将该SQL查询转换为LINQ? - How can I transform this SQL query to LINQ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM