C# 访问非静态成员';UnityEngine.Component.transform';
我想做的是,当我的角色离开一个场景,然后重新进入到它离开的位置时。为此,我想我可以从一个脚本中使用一个变量来保存和更改角色离开的位置,然后在角色进入场景时将其设置为该位置 我想用玩家离开房间时得到的当前玩家位置C# 访问非静态成员';UnityEngine.Component.transform';,c#,unity3d,C#,Unity3d,我想做的是,当我的角色离开一个场景,然后重新进入到它离开的位置时。为此,我想我可以从一个脚本中使用一个变量来保存和更改角色离开的位置,然后在角色进入场景时将其设置为该位置 我想用玩家离开房间时得到的当前玩家位置 using UnityEngine; using System.Collections; public class HomeBaseLevelSwitchOutside : MonoBehaviour { public static Vector3 playerPos =
using UnityEngine;
using System.Collections;
public class HomeBaseLevelSwitchOutside : MonoBehaviour {
public static Vector3 playerPos = GameObject.Find("Player").transform.position;
public GameObject Interaction;
void Start()
{
Interaction.SetActive (false);
}
void OnTriggerStay2D (Collider2D col)
{
Interaction.SetActive (true);
if (Input.GetKeyDown (KeyCode.E))
Application.LoadLevel (5);
HomeBaseLevelSwitchOutside.playerPos = GameObject.Find("Player").transform.position;
}
void OnTriggerExit2D(Collider2D col)
{
Interaction.SetActive (false);
}
}
然后把我的球员放到那个位置
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
//floats
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
//Bools
public bool grounded;
public bool canDoubleJump;
//Stats
public int curHealth;
public int maxHealth = 100;
//References
private Rigidbody2D rb2d;
private Animator anim;
void Start ()
{
Player.transform.position = HomeBaseLevelSwitchOutside.playerPos;
rb2d = gameObject.GetComponent<Rigidbody2D> ();
anim = gameObject.GetComponent<Animator>();
//transform.position = Player.playerPos;
curHealth = maxHealth;
}
void Update ()
{
anim.SetBool ("Grounded", grounded);
anim.SetFloat ("Speed", Mathf.Abs (rb2d.velocity.x));
if (Input.GetAxis ("Horizontal") < -0.1f)
{
transform.localScale = new Vector3(-4, 4, 4);
}
if (Input.GetAxis ("Horizontal") > 0.1f)
{
transform.localScale = new Vector3(4, 4, 4);
}
if (Input.GetButtonDown ("Jump"))
{
if(grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
canDoubleJump = true;
}
else
{
if(canDoubleJump)
{
canDoubleJump = false;
rb2d.velocity = new Vector2 (rb2d.velocity.x,0);
rb2d.AddForce(Vector2.up * jumpPower);
}
}
}
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
if(curHealth <= 0){
Die ();
}
}
void FixedUpdate()
{
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.85f;
float h = Input.GetAxis ("Horizontal");
if(grounded)
{
rb2d.velocity = easeVelocity;
}
rb2d.AddForce ((Vector2.right * speed) * h);
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
void Die(){
//Restart
Application.LoadLevel (Application.loadedLevel);
}
public void Damage(int dmg){
curHealth -= dmg;
gameObject.GetComponent<Animation> ().Play ("redflash");
}
public IEnumerator Knockback(float knockdur, float knockpwr, Vector3 knockdir){
float timer = 0;
while (knockdur > timer) {
timer+=Time.deltaTime;
rb2d.AddForce(new Vector3(knockdir.x * -100, knockdir.y * knockpwr, transform.position.z));
}
yield return 0;
}
}
使用UnityEngine;
使用系统集合;
公共类玩家:单一行为{
//漂浮
公共浮点数maxSpeed=3;
公共浮子速度=50f;
公共浮子跳线功率=150f;
//布尔
公共场所;
公共场所跳高;
//统计数据
公共卫生;
公共int maxHealth=100;
//参考资料
私有刚体2d rb2d;
私人动画师;
无效开始()
{
Player.transform.position=HomeBaseLevelSwitchOutside.playerPos;
rb2d=gameObject.GetComponent();
anim=gameObject.GetComponent();
//transform.position=Player.playerPos;
curHealth=maxHealth;
}
无效更新()
{
anim.SetBool(“接地”,接地);
anim.SetFloat(“速度”,Mathf.Abs(rb2d.velocity.x));
如果(输入轴(“水平”)<-0.1f)
{
transform.localScale=新向量3(-4,4,4);
}
如果(输入轴(“水平”)>0.1f)
{
transform.localScale=新向量3(4,4,4);
}
if(Input.GetButtonDown(“跳转”))
{
如果(接地)
{
rb2d.AddForce(矢量2.up*跳线功率);
canDoubleJump=真;
}
其他的
{
if(canDoubleJump)
{
canDoubleJump=假;
rb2d.velocity=新矢量2(rb2d.velocity.x,0);
rb2d.AddForce(矢量2.up*跳线功率);
}
}
}
如果(curHealth>maxHealth){
curHealth=maxHealth;
}
if(最大速度)
{
rb2d.velocity=新矢量2(maxSpeed,rb2d.velocity.y);
}
if(rb2d.velocity.x<-maxSpeed)
{
rb2d.velocity=新矢量2(-maxSpeed,rb2d.velocity.y);
}
}
空模(){
//重新启动
Application.LoadLevel(Application.loadedLevel);
}
公共空间损坏(int dmg){
curHealth-=dmg;
gameObject.GetComponent().Play(“红光”);
}
公共IEnumerator击退(float knockdur、float knockpwr、Vector3 knockdir){
浮动计时器=0;
while(knockdur>计时器){
timer+=Time.deltaTime;
rb2d.AddForce(新向量3(knockdir.x*-100,knockdir.y*knockpwr,transform.position.z));
}
收益率为0;
}
}
播放器启动方法的第一行:
改变
Player.transform.position = HomeBaseLevelSwitchOutside.playerPos;
到
(您正在以单行为方式访问Transform,而不是Player类上的静态成员)我这样做了,但出现以下两个错误NullReferenceException:Object reference未设置为Object Player.Update()的实例(位于Assets/Scripts/Player.cs:35)和NullReferenceException:Object reference未设置为Object Player.FixeUpdate()的实例(位于Assets/Scripts/Player.cs:80)@ReeceRollings这些与您得到的错误无关-它们出现的原因是您以前的错误已修复,允许您的代码运行并将这些异常抛出其他位置。试着自己解决它们。如果在付出了巨大的努力之后,您无法解决这个问题,那么就发布一个关于堆栈溢出的新问题。(TL;DR:不要把它们加在你现在的问题上,因为它们是不相关的。)
transform.position = HomeBaseLevelSwitchOutside.playerPos;