Search Unity

question about object reference

Discussion in 'Scripting' started by merlinmarijn, Aug 4, 2015.

  1. merlinmarijn

    merlinmarijn

    Joined:
    Aug 3, 2015
    Posts:
    44
    can i get the same effect as this for objects without script?,


    cause some items i want to reference dont have scripts but i still want to have the same functions.
     
  2. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    You can reference objects many ways. Some include using tags , which are slow and normally only used in start and awake functions. You can create the object in that script and save it to a variable as well.
     
  3. merlinmarijn

    merlinmarijn

    Joined:
    Aug 3, 2015
    Posts:
    44
    is there a way to reference objects without script the same as in the picture?
    if not can you give me one of the easiest as example? i am not to good since i started 4 days ago
     
  4. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    Well if you're going to need access to them/their scripts you will need some sort of reference to them.

    Your way is the most visual way of accessing them as you can visually see before the program starts.

    You could get them in the start/awake functions on start up by calling GameObject.FindObjectsWithTag("upgrade")
    This will return a list of all object with the upgrade tag attached to them. Is quite slow so only use at the start of the program. Then use the GetConponent to get the upgrade script it's self for accessing its functions.

    I also assume that the gpcupgrade script is the same and you just made 8 of them for some reason. If so I would just make it one class/script and attach it to those objects.
     
  5. oOHicksyOo

    oOHicksyOo

    Joined:
    Feb 22, 2015
    Posts:
    17
    If you're just trying to get the gameobjects then FindObjectsWithTag will return them as objects , or make an array of GameObject[] instead of the script