Can someone point me to a tutorial on or explain setting up a script to receive an object through the inspector and have access to that objects transform, renderer, other scripts and variables. I cant seen to work it out by pulling apart other scripts. Thanks.
Its unclear what you want? Do you mean you want a script, assigned to a game object to receive other object’s data?
Or do you want the script itself, not inherited from mono behavior to be assigned some kind of information?
You can get alot done, working with a script as a component. There is also alot of find functions which are useful if your script is looking for objects created during run-time.
If you’re working in C# just use public infront of the variable type, then you can assign it in the inspector.
See example?
using UnityEngine;
using System.Collections;
public class Item_Inventory : MonoBehaviour {
//A container for what the object will look like in the game
public GameObject game_object;
//A container for the GuiIcon, Displayed in inventory;
public Texture2D gui_icon;
//a flag for whether the item is on a character
public bool held = false;
//Basically the maximum stack EXAMPLE Grenades Would have Max stack of 6
//0 Means the item is only to be found by itself and doesn't stack.
float Max_Stack = 0;
//Ideally the stack count is greater for stackables but isn't needed for non-stackables
float Stack_Count = 0;
//The consumption time determines wether the item can be used.
//Consumables can be activated when clicked on.
public enum ConsumptionType {Consumable, Permanent_Bonus}
ConsumptionType Con_Type =ConsumptionType.Consumable;
//Currently the bonus is for permanents
public enum BonusType{NA, Str, Dex, Int, Max_Health, Max_Energy}
BonusType bon_type = BonusType.NA;
float Bonus_Amount = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I do use C#, and so far my scripts have been relativity basic. Attached to the same game object so they can easily reference each other or controlling everything from GUI. Or finding a game object and accessing public variables, like you mentioned.
ScriptName scriptName = (ScriptName)GameObject.Find("ObjectName").GetComponent(typeof(ScriptName));
scriptName.variableName = whatEver;
But I would like to set up my script so I can drag and drop an object into the inspector or browse to a component and have access to everything about the came object at runtime. It cant be too much different than what i’m doing, but like I said, I’ve been following conflicting examples and I was looking for a clear example. Thank you for your reply!
That was way easier than I thought. Sometimes it just takes asking the question to think it through.