找回密码
 立即注册
查看: 2625|回复: 38

[脚本] The name `explosion' does not exist in the current context

[复制链接]
发表于 2013-4-3 17:02 | 显示全部楼层 |阅读模式
之前都是使用js编写脚本,今天想使用C#把之前的代码重新整理一下,在写的过程中发现有莫名的错误,不知道是我不知道Unity3D中的C#语法,这些代码在编程的过程中都是Unity 3D提供的API 。刚刚又遇到一个错误提示The name `explosion' does not exist in the current context 。当我把explosion变量放到Update()中就不再报错,但是 我就不能再给爆炸效果赋值了 ,请高手指教!!
发表于 2013-4-3 17:27 | 显示全部楼层
能发个完整点的代码嘛
 楼主| 发表于 2013-4-3 17:42 | 显示全部楼层
谢谢 解决了,是我把变量的类型弄错了
 楼主| 发表于 2013-4-5 16:33 | 显示全部楼层
不知道怎么的,中午代码还正常运行,现在竟然那个什么“没有上下文的错”,Assets/scripts/FishToLeftController.cs(40,38): error CS0103: The name `originPosition' does not exist in the current context  下面是源代码:

using UnityEngine;
using System.Collections;

public class FishToLeftController : MonoBehaviour {
       
        public Transform playerTransform;//小鱼的位置       
        private int times=0;//记录击打RedFish的次数
        private Vector3 originPosition;//获得RedFish的初始值位置       
        private bool isFromRedFish=true;//判断来自RedFish的方向
        // Use this for initialization
        void Start () {
       
        Vector3        originPosition=transform.position;
               
        }
       
        // Update is called once per frame
        void Update () {
               
    float angle=Mathf.Atan((playerTransform.position.y-transform.position.y)/(playerTransform.position.x-transform.position.x));
    Vector3 myVector31=new Vector3(-0.8f*Time.deltaTime,-0.8f*Mathf.Abs(Mathf.Tan(angle))*Time.deltaTime,0.0f);//RedFish在第一象限
        Vector3 myVector34=        new Vector3(-0.8f*Time.deltaTime,0.8f*Mathf.Abs(Mathf.Tan(angle))*Time.deltaTime,0.0f);//RedFish在第四象限       
                 
       
        //Debug.Log("angle  "+angle+"myVector3 "+myVector31+"ATan "+Mathf.Atan(45f)+"Tan "+Mathf.Tan(45f)+"Mathf.Deg2Rad "+Mathf.Deg2Rad*180f+"Mathf.Atan2 "+Mathf.Atan2(1,1)+"Mathf.Rad2Deg "+Mathf.Rad2Deg*3.1415926f);

                myVector31.Normalize();
            if(transform.position.y>playerTransform.position.y)       
                {
                        transform.Translate(0.01f*myVector31);
                        //Debug.Log("if  myVector32 "+myVector31+"playerTransform.position"+playerTransform.position);
                }               
               
               
                myVector34.Normalize();
                if(transform.position.y<playerTransform.position.y)
                {
                        transform.Translate(0.01f*myVector34);               
            }
                if(transform.position.x<=-3.0f)
                {
                  transform.position=originPosition;
                  Debug.Log("chaogu-3.5");
                }
               
   
        }
       
        void OnTriggerEnter(Collider other)
        {
               
                times++;
                if(times>=2)
                {
                  Debug.Log("times "+times);
                  transform.position=originPosition;//这里没有销毁RedFish 只是把它的位置初始化
                  times=0;
                }
               
                if(other.transform.tag=="projectile")
                {
                        Destroy(other.gameObject);
                }
        }
}

发表于 2017-6-15 21:42 | 显示全部楼层
很不错
发表于 2017-6-15 21:56 | 显示全部楼层
好帖就是要顶
发表于 2017-6-15 21:47 | 显示全部楼层
说的非常好
发表于 2017-6-15 21:58 | 显示全部楼层
很好哦
发表于 2017-6-15 21:47 | 显示全部楼层
LZ真是人才
发表于 2017-8-28 17:14 | 显示全部楼层
很不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 00:33 , Processed in 0.607900 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表