NullReferenceException: Object reference not set to an instance of an object
myscript.Update () (at Assets/myscript.cs:33
実際のコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class myscript : MonoBehaviour
{
GameObject[] cubes = new GameObject[4];
GameObject[] gos = new GameObject[4];
// Use this for initialization void Start() { for(int i = 0;i < 4; i++) { cubes[i] = GameObject.Find("Cube" + i); gos[i] = GameObject.Find("GameObject" + i); } } // Update is called once per frame void Update() { Rigidbody rigidbody = GetComponent<Rigidbody>(); foreach(GameObject obj in cubes){ obj.transform.Rotate(new Vector3(1f, 1f, 1f)); } Vector3 v = transform.position; v.y += 2; v.z -= 7; Camera.main.transform.position = v; ←この部分がダメ見たいです if (Input.GetKey(KeyCode.LeftArrow)) { rigidbody.AddForce(new Vector3(-1f, 0f, 0f)); } if (Input.GetKey(KeyCode.RightArrow)) { rigidbody.AddForce(new Vector3(1f, 0f, 0f)); } if (Input.GetKey(KeyCode.UpArrow)) { rigidbody.AddForce(new Vector3(0f, 0f, 1f)); } if (Input.GetKey(KeyCode.DownArrow)) { rigidbody.AddForce(new Vector3(0f, 0, -1f)); } } private void OnTriggerEnter(Collider collider) { if (collider.gameObject.name.StartsWith("Cube")) { for(int i = 0; i < 4; i++) { if(cubes[i] == collider.gameObject) { ParticleSystem ps = gos[i].GetComponent<ParticleSystem>(); ps.Play(); cubes[i].SetActive(false); } } } }
}
コードは https://teratail.com/help#about-markdown の[コードを入力]を使ってください。