方法在创建它的脚本内不起作用

时间:2019-12-08 17:37:31

标签: c# unity3d

虽然在声明了脚本的脚本中使用方法时不起作用,但给出了NullRefferenceException,但在另一个脚本中它却起作用。我正在谈论的方法是:RestartLevel

这是第一个脚本的代码

<form action="addTransaction.php" method="POST">
 ... some form stuff
</form> 

这是该方法起作用的另一个脚本的代码

<?php

 include "conn.php"; 

 $date = date('Y-m-d', strtotime($_POST['date']));


$query= "INSERT INTO transactions (transactionAmount,transactionDate,idCategory,idPayment)
 VALUES (".$_POST["amount"].",'" .$date."' ,".$_POST["category"].",".$_POST["payment"].")";


  if ($link->query($query) === TRUE) {
   //   echo "New record created successfully";
     header("http://localhost/dashboard/HW/home.html");
  } else {
    echo "Error: " . $query . "<br>" . $link->error;
 }
$link->close();
?>

这是在RestartLevel中更改变量的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelInfo : MonoBehaviour {

    public GameObject endGameMenu;
    [SerializeField] GameObject holePref;
    [SerializeField] GameObject holePrefCollider;

    Ball ball;
    TurnOffGroundCollider holeUpCollider;
    Hole hole;
    Score score;
    bool menuIsInstantiated = false;

    private void Start()
    {
        ball = FindObjectOfType<Ball>();
        score = FindObjectOfType<Score>();
        InstanciateHole();
    }

    private void Update()
    {
            if (ball.transform.position.x > -4.9f &&
            ball.GetComponent<Rigidbody2D>().velocity == new Vector2(0, 0))
            {
                    Debug.Log("Condition 1 done");
                    if (GameObject.FindGameObjectsWithTag("Menu").Length < 1)
                    {
                        Debug.Log("Condition 2 done");
                        if (menuIsInstantiated == false)
                        {
                            Debug.Log("Condition 3 done");
                            GameObject menu = InstantiateMenu();
                            menu.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
                            menuIsInstantiated = true;
                        }

                    }     
            }
    }

    public void RestartLevel()
    {
        ball.DefineStartPosition();
        ball.isAbleToPush = true;
        InstanciateHole();
    }

    public GameObject InstantiateMenu()
    {
        Vector3 menuPosition = new Vector3(0, 0, 0);
        GameObject a = Instantiate(endGameMenu, menuPosition, Quaternion.identity);
        return a;
    }

    public void RestartGame()
    {
        //score.ResetScore();
        ball.DefineStartPosition();
        ball.isAbleToPush = true;
        menuIsInstantiated = false;
    }

    public void NewGame()
    {
        Destroy(GameObject.FindGameObjectWithTag("Menu"));
        menuIsInstantiated = false;
        RestartLevel();

    }

    public Vector3 DefineHolePosition()
    {
        float positionX = Random.Range(-1, 8);
        float positionY = 0;
        float positionZ = 0;
        Vector3 position = new Vector3(positionX, positionY, positionZ);
        return position;
    }
    public void InstanciateHole()
    {
        Vector3 position = DefineHolePosition();
        Instantiate(holePref, position, Quaternion.identity);
        Vector3 difference = new Vector3(0, -0.61f, 0);
        Vector3 positionCollider = position + difference;
        Instantiate(holePrefCollider, positionCollider, Quaternion.identity);
    }
}

0 个答案:

没有答案