Home > Enterprise >  Place a block using a method in another script
Place a block using a method in another script

Time:04-04

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?

CodePudding user response:

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.

CodePudding user response:

@Sven_Viking (not sure how to @ properly), 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.

Thanks for the help :)

  • Related