-1

I'm trying to place a block in the game that i am making, I'm trying to call a method from another script and gameObject.

The code i call:

  {
      GameObject instance = Instantiate(tilesprite, new Vector2(x - 100f, y - 33.675f), Quaternion.identity) as GameObject;
      instance.transform.SetParent(this.transform);
      //grid[x, y] = (int)this.transform.position.x, (int)this.transform.position.y];

  }

The code that i call this from:

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

public class CursorsCursorFollow : MonoBehaviour
{
    public Transform target;
    public float smoothSpeed = 0.125f;
    public Vector3 offset;
    public RnadomGeneration2 terrain;
    public bool place;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, 1);// smoothSpeed);
        transform.position = smoothedPosition;
        
    }

    void FixedUpdate()
    {
        Vector3 mousePos = Input.mousePosition;
        //place = Input.GetMouseButtonDown(0);
        if (Input.GetMouseButtonDown(0))
        {
            GameManager.instance.what_tile = 3;

            GameObject tilesprite;
            if (GameManager.instance.what_tile == 0)
            {
                tilesprite = null;
            }
            else if (GameManager.instance.what_tile == 1)
            {
                tilesprite = GameManager.instance.dirt;
            }
            else if (GameManager.instance.what_tile == 2)
            {
                tilesprite = GameManager.instance.grass;
            }
            else if (GameManager.instance.what_tile == 3)
            {
                tilesprite = GameManager.instance.stone;
            }
            else
            {
                tilesprite = null;
            }
            //if (terrain != null && tilesprite != null)
            //{
                terrain.PlaceTile(tilesprite, Mathf.RoundToInt(mousePos.x), Mathf.RoundToInt(mousePos.y)); <line 59>
            //}
        }
    }
}

Get the error: NullReferenceException: Object reference not set to an instance of an object CursorsCursorFollow.FixedUpdate () (at Assets/Scripts/CursorsCursorFollow.cs:59)

What is wrong with my code?

Help
  • 29
  • 8
  • That doesn't look like all of the error message? Also from the information here it isn't possible for us to know which line is line 57 of CursorsCursorFollow.cs. – Sven Viking Apr 04 '22 at 01:03
  • 1
    sorry line 57 is "terrain.PlaceTile(tilesprite, Mathf.RoundToInt(mousePos.x), Mathf.RoundToInt(mousePos.y));" i'll put this in the question – Help Apr 04 '22 at 02:03
  • Is there really no more to the error message? Usually it’d be something more like “NullReferenceException: Object reference not set to an instance of an object CursorsCursorFollow.FixedUpdate () (at Assets/Scripts/CursorsCursorFollow.cs:57)“ – Sven Viking Apr 04 '22 at 02:26
  • that was the only error message that came up and all there was to it. i copy and pasted edit: sorry i must've accidentally not done it all: "NullReferenceException: Object reference not set to an instance of an object CursorsCursorFollow.FixedUpdate () (at Assets/Scripts/CursorsCursorFollow.cs:57)" – Help Apr 04 '22 at 02:27
  • Weird. Only thing I can suggest from what’s here is to try putting `if( terrain != null && tilesprite != null )` in front of the terrain.PlaceTile() line and see if that prevents the error since it’s quite likely caused by one of those being null (and then if so, try to work out why it's null and whether you want it to be). Adding the PlaceTile() code to the question could help. – Sven Viking Apr 04 '22 at 02:36
  • PlaceTile() is the one at the top, sorry I'm not very good at this yet, but I'll get the hang of it soon hopefully – Help Apr 04 '22 at 02:44
  • now there is no error but a block won't place that is dirt. – Help Apr 04 '22 at 02:47
  • If you include the first line of PlaceTile it’ll both make it clearer what it is and let us know what parameters it accepts. Anyway sounds like GameManager.instance.dirt I’d null for some reason so check there. Maybe the asset just needs to be dragged into the GameManager component or something. – Sven Viking Apr 04 '22 at 03:03
  • i'll update the question and check – Help Apr 04 '22 at 03:06
  • the GameManager.instance.dirt is not null, it is set to a prefab called dirt – Help Apr 04 '22 at 03:12
  • (That’s assuming non-dirt tiles are working, by the way. If none are working, it could still be `terrain` that’s null or something. The line `GameManager.instance.what_tile = 1;` higher up seems like it would make *all* of them dirt.) – Sven Viking Apr 04 '22 at 03:14
  • i'm just trying to see if it would work with dirt first so that maybe they all would work, – Help Apr 04 '22 at 03:17
  • Try commenting out the null check temporarily and checking the error message again. If you click an error message in the Unity log window it should have a lot of text in the lower pane as in [this image](https://answers.unity.com/storage/temp/5522-error.jpg). Really weird if it doesn’t. – Sven Viking Apr 04 '22 at 03:19
  • image up in the question of my error – Help Apr 04 '22 at 03:21
  • The problem is definitely that *something* is null, then. Maybe it’s the terrain variable. – Sven Viking Apr 04 '22 at 03:47
  • so the terrain variable is a script so that can't be null – Help Apr 04 '22 at 04:34
  • terrain is a variable to hold an instance of the “RnadomGeneration2” class and will be null if not assigned e.g. by dragging the appropriate object into the terrain variable in the inspector, or via terrain=GetComponent< RnadomGeneration2>(); – Sven Viking Apr 04 '22 at 04:52
  • (Or if it’s not a Unity component/MonoBehavior, using `terrain = new RnadomGeneration2();` ) – Sven Viking Apr 04 '22 at 05:05
  • ok now i get no errors, but nothing will clone, (instantiate) – Help Apr 04 '22 at 07:41
  • Try to look up how you can set up [debugging](https://youtu.be/d0815qbx3BA) for Unity so you can step through your code and see what’s happening exactly. – Sven Viking Apr 04 '22 at 08:08

2 Answers2

0

You are not mentioning what row you are getting the error message at which makes it harder. But a null reference is usually really easy to solve, it means you haven't declared something that it is trying to run on the row that gives the error. Possibly the terrain, tilssprite or the mousePos.

eligolf
  • 1,239
  • 4
  • 18
  • i said line 59, the stuff is assigned but it won't instantiate now, and it says where line 59 on there :) – Help Apr 04 '22 at 07:43
  • If you get a null reference then it is not assigned. I see you now added a comment to where line 59 is, good. So either the terrain, tilesprite or mousepos is null. Before that line, create an if terrain == null and do the same for all the others, then you will see which one is null. Then you figure out why it is null and if it should be something else. – eligolf Apr 04 '22 at 07:55
  • i don't get any null reference exceptions now, but it won't do what i want it to do, I want it to clone "GameManager.instance.dirt" if "GameManager.instance.what_tile" is 1, there are no null exceptions but it just won't clone. – Help Apr 04 '22 at 10:36
  • ok it does make a clone and everything. i just put Debug.Log("placed tile"); when it was called – Help Apr 04 '22 at 10:41
-2

@Sven_Viking has provided the answer.

if( terrain != null && tilesprite != null )

This code helped as it created a clone, and it made sure that a null wasn't passed through the variable.

Help
  • 29
  • 8
  • This code doesn't create a clone. It just assures that terrain or tilesprite is not null. Those variables should never be null when you click on a terrain, so something else should be wrong in your code. – eligolf Apr 04 '22 at 14:59
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 05 '22 at 01:39
  • @eligolf If I understand correctly it seems like the question was unanswerable as written because the root cause was outside of the code provided, but comments here allowed him to identify and solve the real problem. – Sven Viking Apr 05 '22 at 05:07