NullReferenceException: Object reference not set to an instance of an object
Player_Interaccion.Update () (at Assets/Script/Player_Interaccion.cs:40)
How can i make it work ???
Where is my error ???
I’m learning how to use Raycast from youtube and with the unity manual version 2021.3.33f.
I try many way to set an instance of my “script” in game and also in my script.
I asume that " Null Reference Exception" means that at some point i make it work by script but something fail in my scene or i dont know, i spent two days drinking Té and watching youtube raycast and nothing make it work.
I handle very, very basic concepts. Please don’t give me very complex solutions, my goal is to take it step by step.
I’ve built 2 scripts, the first one is called “Enemys” and the second one “Player_Interaccion”
The “Enemys” script is added to the components of a UnityLearn Microgame FPS Hover_Bot. The “Player_Interaccion” script is added to the “MainCamera” of the GameObject “Player”.
My intention is to give attributes to a game object that will be an enemy, and then with a raycast detect the game object to access that script component so that it can display on console the attributes that were added through it.
using System.Collections;
using System.Collections.Generic;
using UnityEditor.ShaderGraph.Internal;
using UnityEngine;
public class Enemys : MonoBehaviour
{
//Aqui se definen los atributos de la clase enemy, para luego utilizar sus métodos y atributos
// en una clase que heredara de Enemys
public float Level;
public float Healt;
public float Mana;
public float Defense;
public float Attack;
public float Strength;
LayerMask mask;
public float distanceRay;
// Start is called before the first frame update
// Update is called once per frame
// estas ecuaciones para definir a los atributos de cada actor, deben ser definidos en la propia,
//clase de cada actor donde heredaran estos metodos y atributos.
public float CalculoHealt ()
{
Healt = 15 + Level * 4;
return Healt;
}
public float CalculoMana()
{
Mana = 3 + Level / 2 + 1;
return Mana;
}
public float CalculoDefense() {
float healt1 = Healt / 5;
Defense = healt1 + Level / 2;
return Defense;
}
public float CalculoAttack() {
float attack1 = Strength ;
Attack = Level * 2 + 3;
return Attack;
}
private float CalculoStrength() {
Strength = Level * 2 + 1;
return Strength;
}
public void EnemysInfo() {
Debug.Log("Healt: " + Healt + " .");
Debug.Log("Mana: " + Mana + " .");
Debug.Log("Defense: " + Defense + " .");
Debug.Log("Attack: " + Attack + " .");
Debug.Log("Strength: " + Strength + " .");
Debug.Log("Level: " + Level + " .");
}
private void Start()
{
distanceRay = 50;
mask = LayerMask.GetMask("LayerEnemysInfo");
CalculoStrength();
CalculoHealt();
CalculoDefense();
CalculoMana();
CalculoAttack();
EnemysInfo();
}
public void Update()
{
/*
RaycastHit hit;
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward * distanceRay);
*/
// la variable "hit" del tipo RaycastHit es la que permite al objeto al cual se añade el script
// emitir el rayo.
// el rayo
// "out hit" es lo que almacena los valores.
//(Physics.Raycast(ray,out hit)) es una de las estructuras del Raycast que permite obtener información
// del gameObjet con el que colisiona.
/*
* if (Physics.Raycast(ray, out hit, 50f, mask))
{
Debug.DrawRay(ray.origin, ray.direction * 2.5f, Color.yellow);
EnemysInfo();
*/
/*
* Debug.Log("Distancia: " + hit.distance);
Debug.Log("Punto de Impacto: " + hit.point);
Debug.Log("Nombre: " + hit.transform.name );
*/
//hit.transform.gameObject.GetComponent<MeshRenderer>().enabled = false;
//hit.transform.gameObject.GetComponent<MeshRenderer>().material.color = Color.magenta;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;
using Unity.VisualScripting;
public class Player_Interaccion : MonoBehaviour
{
Enemys enemys;
Enemys misEnemys;
private void Start()
{
}
void Update()
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
// la variable "hit" del tipo RaycastHit es la que permite al objeto al cual se añade el script
// emitir el rayo.
// el rayo
// "out hit" es lo que almacena los valores.
//(Physics.Raycast(ray,out hit)) es una de las estructuras del Raycast que permite obtener información
// del gameObjet con el que colisiona.
if (Physics.Raycast(ray,out hit))
{
Debug.DrawRay(ray.origin, ray.direction * 2.5f, Color.yellow);
Debug.Log("Distance: " + hit.distance);
Debug.Log("Target Point: " + hit.point);
Debug.Log("Name: " + hit.transform.name);
GameObject targetRaycast = hit.transform.gameObject;
enemys = targetRaycast.GetComponent<Enemys>();
enemys.EnemysInfo();
float misEnemys = hit.transform.GetComponent<Enemys>().Healt;
Debug.Log(misEnemys);
var lesEnemys = hit.transform.GetComponent<Enemys>();
lesEnemys.EnemysInfo();
//hit.transform.gameObject.GetComponent<MeshRenderer>().enabled = false;
//hit.transform.gameObject.GetComponent<MeshRenderer>().material.color = Color.magenta;
}
}
}
9570097–1354162–Enemys.cs (3.16 KB)
9570097–1354165–Player_Interaccion.cs (1.66 KB)