[–] MrKequc 0 points 0 points (+0|-0) ago 

Unity is a strange animal but once you wrap your head around the concept of game objects basically replacing a large amount of scripting it gets pretty easy to use. Hard to keep all of your game objects organised.

[–] FlamingoBumblebee 0 points 0 points (+0|-0) ago  (edited ago)

if you have a player script on an object (the player, presumably) and you want to access it from another script, you would declare a variable in the other script like:


private Player player;

and in Start() (or Awake(), or OnEnable(), or OnTriggerEnter(), or in another method/function, etc (although in any method other than Start, OnEnable, or Awake you would not declare the variable beforehand unless you were referencing the cached global variable you declared in the script)) you would add:

player = FindObjectOfType(typeof(Player)) as Player;

and then you would use the player variable to access that script.

those were just examples, but you shouldn't use FindObjectOfType in methods/functions directly. you should store a reference to the Player object, or whatever object, in Start() or Awake() and then store a reference to it in a variable in your method/function and call the necessary part of the script from there.

Edit: as far as learning, the Unity documentation and the forums over there can offer tons of help. the docs aren't the clearest at times but usually if something isn't explained in the best detail you can find some questions and answers about it in the forums or q&a section. happy developing!

Edit 2: regarding the FindObjectOfType: it's an expensive call so you want to use those when you load everything. if you want to reference another script on the same object, such as PlayerWeapon, you would use:

GetComponent(typeof(PlayerWeapon)) as PlayerWeapon;

those calls are a lot cheaper, but if you're using it a lot in one script you should still create a reference to it and then if you want you can create a variable with it in the method/function.

local variables get stored on the heap and global variables get stored on the stack.

[–] AnotherNewAlt [S] 0 points 0 points (+0|-0) ago 

Ok, as I was typing up the problem I was having the next run I did solved it.

However there's still something important I need to know for general knowledge

player = FindObjectOfType(typeof(Player)) as Player;

How do I know the data type of the object I'm trying to access? I need the exact name. I see things in the object, that I made from the menu that indicated I was making a sprite object, the allude to it being a sprite like "sprite renderer", but if I'm trying to use the line of code you have above how do I knew the exact name to write in there so I can find what I'm looking for? I would think it would be at the top of the Inspector, but I don't think that's right because I can type anything I want in there, including spaces. And after that I see nothing consistent between the objects that actually says the name of the object in it.

[–] FlamingoBumblebee 0 points 0 points (+0|-0) ago  (edited ago)

the SpriteRenderer's type is SpriteRenderer so you would access it like:

private SpriteRenderer spriteRenderer;

and define it as:

spriteRenderer = GetComponent(typeof(SpriteRenderer)) as SpriteRenderer;

also anything that has a script on it has a type of NameOfScript.

if you want to access a script called VoatGoat you would access it like:

GetComponent(typeof(VoatGoat)) as VoatGoat;

use GetComponent when the script you want is on the same object as the script you're calling it from.

if you want the object the VoatGoat script is on you would do:

private GameObject voatGoat;

and define it as:

voatGoat = FindObjectOfType(VoatGoat)).gameObject as GameObject;