Вот код:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Figure : MonoBehaviour {
GameObject[] Tiles = new GameObject[4];
RaycastHit[] Enemies = new RaycastHit[4];
RaycastHit hit;
private Renderer renderer;
void Start () {
renderer = GetComponent<Renderer> ();
}
void OnMouseDown(){
ShowPosMoves();
}
public void ShowPosMoves(){
try {
Tiles [0] = GameObject.Find ((this.transform.position.x + 1).ToString () + (this.transform.position.z));
Tiles [1] = GameObject.Find ((this.transform.position.x - 1).ToString () + (this.transform.position.z));
Tiles [2] = GameObject.Find ((this.transform.position.x).ToString () + (this.transform.position.z + 1));
Tiles [3] = GameObject.Find ((this.transform.position.x).ToString () + (this.transform.position.z - 1));
foreach (GameObject go in Tiles) {
go.GetComponent<TileScript> ().CanBePressed = true;
go.GetComponent<Renderer> ().material.color = Color.yellow;
}
Debug.Log("Первая часть прошла успешно");
} catch {
}
if (Physics.Raycast (this.transform.position, Vector3.forward, out hit, FigSpd, 8))
Enemies [0] = hit;
if (Physics.Raycast (this.transform.position, Vector3.back, out hit, FigSpd, 8))
Enemies [1] = hit;
if (Physics.Raycast (this.transform.position, Vector3.right, out hit, FigSpd, 8))
Enemies [2] = hit;
if (Physics.Raycast (this.transform.position, Vector3.left, out hit, FigSpd, 8))
Enemies [3] = hit;
for(int i = 0;i < Enemies.Length;i++){
Enemies[i].transform.gameObject.GetComponent<Renderer>().material.color = Color.red;
}
}
using System.Collections.Generic;
using UnityEngine;
public class Figure : MonoBehaviour {
GameObject[] Tiles = new GameObject[4];
RaycastHit[] Enemies = new RaycastHit[4];
RaycastHit hit;
private Renderer renderer;
void Start () {
renderer = GetComponent<Renderer> ();
}
void OnMouseDown(){
ShowPosMoves();
}
public void ShowPosMoves(){
try {
Tiles [0] = GameObject.Find ((this.transform.position.x + 1).ToString () + (this.transform.position.z));
Tiles [1] = GameObject.Find ((this.transform.position.x - 1).ToString () + (this.transform.position.z));
Tiles [2] = GameObject.Find ((this.transform.position.x).ToString () + (this.transform.position.z + 1));
Tiles [3] = GameObject.Find ((this.transform.position.x).ToString () + (this.transform.position.z - 1));
foreach (GameObject go in Tiles) {
go.GetComponent<TileScript> ().CanBePressed = true;
go.GetComponent<Renderer> ().material.color = Color.yellow;
}
Debug.Log("Первая часть прошла успешно");
} catch {
}
if (Physics.Raycast (this.transform.position, Vector3.forward, out hit, FigSpd, 8))
Enemies [0] = hit;
if (Physics.Raycast (this.transform.position, Vector3.back, out hit, FigSpd, 8))
Enemies [1] = hit;
if (Physics.Raycast (this.transform.position, Vector3.right, out hit, FigSpd, 8))
Enemies [2] = hit;
if (Physics.Raycast (this.transform.position, Vector3.left, out hit, FigSpd, 8))
Enemies [3] = hit;
for(int i = 0;i < Enemies.Length;i++){
Enemies[i].transform.gameObject.GetComponent<Renderer>().material.color = Color.red;
}
}
Ошибку (NullReferenceException) выдаёт на строке
Синтаксис:
Используется csharp
Enemies[i].transform.gameObject.GetComponent<Renderer>().material.color = Color.red;
При нажатии на фигуру
Надеюсь на помощь и заранее спасибо