As said in the header. Can anyone help?
I can give you codes if needed.
Hi @aybarsmete1
If you bothered to google for “Object reference not set to an instance of an object” you would get an answer explaining the reason why this happens.
And usually it is necessary to see the code.
I searched like 100 pages and nothing. I am going to give you the code now:
This is the code that I want to use bool from:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class mouseOver : MonoBehaviour
{
public bool mouseisOver;
// Start is called before the first frame update
void Start()
{
}
private void OnMouseOver()
{
mouseisOver = true;
}
// Update is called once per frame
void Update()
{
}
}
And this is the code I want to use mouseisOver:
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;
using System.Collections;
using Cinemachine;
public class ingameBuild : MonoBehaviour
{
public Tile highlightTile;
public Tile emptyTile;
public Tilemap highlightMap;
public TilemapCollider2D tlc;
public Rigidbody2D gobject;
public Text selectedText;
public BoxCollider2D sore, player;
GameObject cube;
[SerializeField]
public CinemachineVirtualCamera cm;
public Tile[] tiles = new Tile[7];
bool isInside;
bool is7;
bool is3;
bool is5;
bool isTouchingCollider;
int[] counts = new int[7];
mouseOver mo;
int selectedTile;
string[] names = new string[] { "Grass", "Dirt", "Red Tile", "Blue Tile", "Pillar", "Mossy Pillar", "Wooden Chest" };
private void Start()
{
selectedTile = 0;
cube = GameObject.FindGameObjectWithTag("turoh");
cube.GetComponent<mouseOver>();
}
private void Update()
{
if(gobject.IsTouching(player))
{
isInside = true;
}
else
{
isInside = false;
}
if (cm.m_Lens.OrthographicSize == 7)
{
is7 = true;
is5 = false;
is3 = false;
}
if (cm.m_Lens.OrthographicSize == 5)
{
is5 = true;
is7 = false;
is3 = false;
}
if (cm.m_Lens.OrthographicSize == 3)
{
is3 = true;
is5 = false;
is7 = false;
}
if (selectedTile < 0)
{
selectedTile = counts.Length - 1;
}
if (selectedTile > counts.Length - 1)
{
selectedTile = 0;
}
selectedText.text = "Selected Tile: " + names[selectedTile];
gobject.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
if(gobject.IsTouching(tlc))
{
isTouchingCollider = true;
}
if (Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetAxis("Mouse ScrollWheel") < 0 & is5 | Input.GetAxis("Mouse ScrollWheel") < 0 & is3)
{
cm.m_Lens.OrthographicSize += 2;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 & is5 | Input.GetAxis("Mouse ScrollWheel") > 0 & is7)
{
cm.m_Lens.OrthographicSize -= 2;
}
}
}
// do late so that the player has a chance to move in update if necessary
private void LateUpdate()
{
// get current grid location
Vector3Int currentCell = highlightMap.WorldToCell(transform.position);
// if the position has changed
if (Input.GetMouseButton(1))
highlightMap.SetTile(currentCell, tiles[selectedTile]);
if (Input.GetMouseButton(0))
highlightMap.SetTile(currentCell, emptyTile);
if (!Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
selectedTile++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
selectedTile--;
}
}
}
}
I want to use mouseisOver lateupdate, and as you can see I tried few codes to see if it works. I would be pleased if you help me since this is the code that blocks me from advancing.
And on what line you get the error?
You should see something like in your debug console:
MyGame.Start () (at Assets/MyGameClass.cs:17)
The error has to do with you not having object reference set, like it says.
Usually, either your object isn’t tagged correctly or whatever method you use to find the object, you don’t find it because of it doesn’t exist in scene, it has incorrect tag, and you end up having empty variable, from which you try to use some method, and then you will get the error you get.
I didn’t read your code, I’m waiting to see if you come up with the line number where you get the error. You should also do Debug.Logs after each Find etc to see if you actually found your object when you get this kind of errors. Or use debugger.
This line:
cube.GetComponent<mouseOver>();
Should probably be:
mo = cube.GetComponent<mouseOver>();
“mo” variable is probably null, but I don’t see you trying to use it anywhere, so it could be any other reference causing the error.
Therefore he should find the line where he gets the error…
Obviously, yes. Just pointing out one glaring coding mistake that could cause the issue.
Oh sorry, I didn’t include the line that I get error.
This is the line:
if(!mo.mouseisOver) <-----
Now I updated the one with code.
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;
using System.Collections;
using Cinemachine;
public class ingameBuild : MonoBehaviour
{
public Tile highlightTile;
public Tile emptyTile;
public Tilemap highlightMap;
public TilemapCollider2D tlc;
public Rigidbody2D gobject;
public Text selectedText;
public BoxCollider2D sore, player;
GameObject cube;
[SerializeField]
public CinemachineVirtualCamera cm;
public Tile[] tiles = new Tile[7];
bool isInside;
bool is7;
bool is3;
bool is5;
bool isTouchingCollider;
int[] counts = new int[7];
mouseOver mo;
int selectedTile;
string[] names = new string[] { "Grass", "Dirt", "Red Tile", "Blue Tile", "Pillar", "Mossy Pillar", "Wooden Chest" };
private void Start()
{
selectedTile = 0;
cube = GameObject.FindGameObjectWithTag("turoh");
mo = cube.GetComponent<mouseOver>();
}
private void Update()
{
if(gobject.IsTouching(player))
{
isInside = true;
}
else
{
isInside = false;
}
if (cm.m_Lens.OrthographicSize == 7)
{
is7 = true;
is5 = false;
is3 = false;
}
if (cm.m_Lens.OrthographicSize == 5)
{
is5 = true;
is7 = false;
is3 = false;
}
if (cm.m_Lens.OrthographicSize == 3)
{
is3 = true;
is5 = false;
is7 = false;
}
if (selectedTile < 0)
{
selectedTile = counts.Length - 1;
}
if (selectedTile > counts.Length - 1)
{
selectedTile = 0;
}
selectedText.text = "Selected Tile: " + names[selectedTile];
gobject.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y));
if(gobject.IsTouching(tlc))
{
isTouchingCollider = true;
}
if (Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetAxis("Mouse ScrollWheel") < 0 & is5 | Input.GetAxis("Mouse ScrollWheel") < 0 & is3)
{
cm.m_Lens.OrthographicSize += 2;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 & is5 | Input.GetAxis("Mouse ScrollWheel") > 0 & is7)
{
cm.m_Lens.OrthographicSize -= 2;
}
}
}
// do late so that the player has a chance to move in update if necessary
private void LateUpdate()
{
// get current grid location
Vector3Int currentCell = highlightMap.WorldToCell(transform.position);
// if the position has changed
if(!mo.mouseisOver)
{
if (Input.GetMouseButton(1))
highlightMap.SetTile(currentCell, tiles[selectedTile]);
}
if (Input.GetMouseButton(0))
highlightMap.SetTile(currentCell, emptyTile);
if (!Input.GetKey(KeyCode.LeftControl))
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
selectedTile++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
selectedTile--;
}
}
}
}
PS: It still gives the same error.
Did you Debug.Log or Assert if you got the cube on line 32?
If you did get it, did you Debug.Log or Assert if you got the mouseOver component on line 33?
You really need to add some Debug.Logs to this. When i need to verify that I am actually grabbing a component in Start or Awake I always add a check to make sure it grabs it. In your start try:
if(mo == null)
{
Debug.Log("We do NOT have the mouseOver component");
}
else
{
Debug.Log("We DO HAVE the mouseOver component");
}
Begin with that and make sure it is calling the correct component. If it still isnt working somewhere else in the code, add more Debug.Logs to find out what is calling and what isnt.
This is how I’d try to do that.
pubic static bool mouseIsOver;
if(mouseOver.mouseIsOver == false)
{
if (Input.GetMouseButton(1))
highlightMap.SetTile(currentCell, tiles[selectedTile]);
}