需要一个对象引用来访问非静态成员'UnityEngine.Component.transform'
问题描述:
我想要做的就是让我的角色离开一个场景然后重新进入它的位置为了做到这一点,我想我可以用一个脚本中的变量来保存和修改离开位置的字符,然后在进入场景时将其设置为该字符。需要一个对象引用来访问非静态成员'UnityEngine.Component.transform'
我想使用当前玩家的位置是当玩家离开房间时得到的。
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;
}
}
答
在播放器的启动方法,第一行:
变化
Player.transform.position = HomeBaseLevelSwitchOutside.playerPos;
到
transform.position = HomeBaseLevelSwitchOutside.playerPos;
(您正在访问变换在MonoBehaviour,而不是在播放器类的静态成员)
我这样做,但我得到这两个错误的NullReferenceException:未将对象引用设置到对象 Player.Update的一个实例( )(在资产/脚本/ Player.cs:35) 和的NullReferenceException:对象没有设置为一个对象 Player.FixedUpdate()的一个实例(在资产/脚本/ Player.cs:80) –
@ReeceRollings那些AREN与你遇到的错误无关 - 他们显示的原因是因为你以前的错误已被修复,允许你的代码运行并在别处抛出这些异常。尝试自己解决它们。如果花费很大的努力后,你无法在Stack Overflow上发布一个新问题。 (TL; DR:不要把它们加到你现在的问题上,因为它们不相关。) – Serlite