-1
我想要做的就是讓我的角色離開一個場景然後重新進入它的位置爲了做到這一點,我想我可以用一個腳本中的變量來保存和修改離開位置的字符,然後在進入場景時將其設置爲該字符。需要一個對象引用來訪問非靜態成員'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;
}
}
我這樣做,但我得到這兩個錯誤的NullReferenceException:未將對象引用設置到對象 Player.Update的一個實例( )(在資產/腳本/ Player.cs:35) 和的NullReferenceException:對象沒有設置爲一個對象 Player.FixedUpdate()的一個實例(在資產/腳本/ Player.cs:80) –
@ReeceRollings那些AREN與你遇到的錯誤無關 - 他們顯示的原因是因爲你以前的錯誤已被修復,允許你的代碼運行並在別處拋出這些異常。嘗試自己解決它們。如果花費很大的努力後,你無法在Stack Overflow上發佈一個新問題。 (TL; DR:不要把它們加到你現在的問題上,因爲它們不相關。) – Serlite