Search Unity

Static function

Discussion in 'Scripting' started by denis-andreevich, Jun 19, 2019.

  1. denis-andreevich

    denis-andreevich

    Joined:
    Aug 21, 2016
    Posts:
    50
    In my game I have a prefab with the tree. The script of this prefab contains static fields and static methods. This is done in order to be able to safely set at the level of any number of trees and get access to functions and all such other things. During the execution of the game, when you need to perform any action on the tree, I get an error:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    .

    How can I directly in the code indicate which prefab I need and where its respawn point will be?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    We're going to need more information / your code to help.
     
  3. ThySpektre

    ThySpektre

    Joined:
    Mar 15, 2016
    Posts:
    362
    More info will be needed, but in general if you want to have access to one particular tree in the scene, static fields and methods are not what you are looking for. They are shared across all instances of the class.