Search Unity

Method calling

Discussion in '2D' started by iwannafly35, May 29, 2017.

  1. iwannafly35

    iwannafly35

    Joined:
    May 22, 2017
    Posts:
    13
    So I made this game, where you trigger an object which destroys itself when triggered and displays canvas where is one fact and true/false buttons, so when user interacts with true statement, the object in front of the player which blocks the way of player, should also destroy itself. So, my problem is how can/should call the methods from other scripts so the code would work.
    Here's the code from the scritps where the method is made, and called.

    public class TriggerScript2
    {
    public GameObject spriteObj;

    public void destroySprite()
    {
    Destroy(spriteObj);
    }
    }

    public void selectTrue ()
    {
    if (currentQ.isTrue && Time.timeScale == 0)
    {
    Time.timeScale = 1;
    myCanvas.SetActive(false);
    setRandQ();
    objSprite.destroySprite(); //the line where the error occurs "Object reference not set to an instance of an object"
    }
     
  2. Donay

    Donay

    Joined:
    Apr 28, 2017
    Posts:
    70
    How do you work out which block is infront of the player?
    When posting code it helps to if you use code tags (place '[ code ]' before and '[ /code ]' after, without the spaces and '.
     
  3. iwannafly35

    iwannafly35

    Joined:
    May 22, 2017
    Posts:
    13
    I made public variable "public TriggerScript2 objSprite;", and connected the sprite with this in inspector.