0
\$\begingroup\$

I am trying to fix an error that says "Object reference not set to an instance of an object". The purpose of this script is to spawn a prefab that follows the mouse when you click a UI button. The script works fine despite showing the error(yellow triangle), but when I add a line referencing another scripts array(marked >>>) in the HandleNewObjectHotkey function) the game refuses to run(red stop sign). For context, this script shown is attached to an empty game object. The button retrieves the script from this game object, and specifically uses the HandleNewObjectHotkey Function. I would like to know how I can fix the placeable Object Prefab from producing an error, or a suggestion to bypass it.

Things I've tried: making placeableObject prefab public, removing serialize field, attaching the prefab to the script itself in assets, change everything from private to public.

[SerializeField]
private GameObject placeableObjectPrefab;           // shows error here in visual studio, suggesting to make read-only (doesn't fix the problem) 

private GameObject currentPlaceableObject;
private float mouseWheelRotation;
public int objectsSpawned;

// -------------- Money stuff

private float money;
private MoneyDisplay moneyDisplay;
private ObjectCostDisplay objectcostDisplay;

private void Awake()
{
    moneyDisplay = GameObject.FindObjectOfType<MoneyDisplay>();
    objectcostDisplay = GameObject.FindObjectOfType<ObjectCostDisplay>(); 
}

private void Update()
{

    if (currentPlaceableObject != null)
    {
        MoveCurrentPlaceableObjectToMouse();
        RotateFromMouseWheel();
        ReleaseIfClicked();
    }
}

private void ReleaseIfClicked()
{
    if (Input.GetMouseButtonDown(0))
    {
        currentPlaceableObject = null;
    }
}

private void RotateFromMouseWheel()
{
    mouseWheelRotation += Input.mouseScrollDelta.y;
    currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 10f);
}

private void MoveCurrentPlaceableObjectToMouse()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    RaycastHit hitInfo;
    if (Physics.Raycast(ray, out hitInfo))
    {
        currentPlaceableObject.transform.position = hitInfo.point;
        currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
    }
}

public void HandleNewObjectHotkey()                                             //error says issue is in this function. 
{
    money = objectcostDisplay.CostsList[0];   // >>>   adding this is what causes unity to refuse to run the game due to error mentioned. Deleting this lets game run, error still present. 

    if (objectsSpawned <= 3 && money <= moneyDisplay.currentMoney)              
    {
        if (currentPlaceableObject == null )
        {
            currentPlaceableObject = Instantiate(placeableObjectPrefab);        
            objectsSpawned++;
            moneyDisplay.UpdateMoney(money); 
        }
    }

    if(objectsSpawned == 3 && money <= moneyDisplay.currentMoney)
    {
        Debug.Log("You have reached the limit for this item!"); 
    }

    if(objectsSpawned <= 3 && money > moneyDisplay.currentMoney)
    {
        Debug.Log("You don't have enough money for this!"); 
    }
}

}

\$\endgroup\$
2
  • \$\begingroup\$ If your game won't even enter play mode, that means you have a compiler error. "Object reference not set to an instance of an object" is a runtime error. So while it might also be a problem, it's not the error responsible for stopping your game from running. You should consult your console log and edit your question to include any compiler errors you find, and the relevant code sections. \$\endgroup\$
    – DMGregory
    Jun 25 '20 at 11:49
  • \$\begingroup\$ I'd just like to say that after about 4 days of researching/ trying to problem solve this issue (including looking at the reference link I was given on this post) the issue fixed itself when I put the empty game object with the script attached as a child of another object which houses the button I was using to activate it. Doing this fixed the issue. \$\endgroup\$
    – Jonathan L
    Jun 27 '20 at 6:15

Browse other questions tagged .