Hi is there a way to attach a Script as public in the same way you might do a Gameobject,Transform etc;
Public GameObject go;
Public Transform trans;
Public Script thisscript // anyway to do something like this?
If your question refers to ‘drag and drop a scrip in the inspector’, yes you can. Just drag and drop a gameobject with the script attached to it onto the slot of your script-type.
Yea that way i know was hoping there was a way to skip the middleman.
Can’t think off any other way, because scripts derived from Monobehaviour are usually attached to gameobjects and this way they’re instantiated (well, you know that… i’m pretty sure ), so for your case, the question that comes up is where would you like to grab the instances from, if not from a gameobject?
Sure, you can have a variable of a class type.
public MyScriptName myScript;
If you’re asking to be able to add a script that isn’t on a GameObject into a variable in the inspector, I don’t believe that’s possible, and you’ll probably want to avoid designing components in this manner in Unity.
You could have some sort of Enum/drop-down, and then have your component’s functionality be different depending on which Enum is chosen (assuming that’s what you’re going for), or something more object-oriented, but you’ll likely want to just add the script to the GameObject itself if you’re looking to have your object’s functionality depend on the script.
Ok basically I’m trying to make a rpg. So i am writing a script containing all possible spells in game which i want attached to each magic user i might have. Then i will have a character specific script for each individual mage that would govern what spells they can actually use. I 'd like them to be able to have quick access to the spells as if they were directly attached to them if i could have a direct reference in there specific scripts then it would save me having to use find or getcomponent or having to copypaste this script countless times for each individual mage.
Basically just want all the info in the generic (universal)script including all references to gameobjects variables and the like that i can run against the variables objects i have on my unique mage scripts.
II have never done a getcomponent on a script before If it is attached to a gameobject will i need to use getcomponent in children? and would i have access to everything in the script including drag and drop GO’s variables etc with this call. Basically I’m thinking of having a whole lot of functions such as below
void fireball(){
if (tier==2){
Vector3 direction = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation (direction);
Instantiate (fire, transform.position, rotation);
damage=Random.Range (40,70);onfire=Random.Range (1,100);
if (onfire<5){ damage=Random.Range (5,8);duration=15;
Instantiate (burning, transform.position, rotation);
}
If that’s the case, you might want to just have the information in a class that isn’t a component, or attached to a GameObject that isn’t destroyed when you change scenes. I’d probably do the latter, since if it’s a component you’ll be able to edit the properties of spells in the inspector, depending on your implementation of the component itself.
There’s nothing wrong with using ‘GetComponent<>’ unless you’re doing it on every Update call, by the way.
So this is what i have tried. Attached my generic script to a GO
using UnityEngine;
using System.Collections;
[code=CSharp]
using UnityEngine;
using System.Collections;
public class magicspells : MonoBehaviour {
public GameObject fire;
public Transform target;
public GameObject Target;
int duration;
int damage;
int onfire;
int tier;
public magicuser myScript; //magicuser name of specific script
public int strength=10;
public void fireball(){
if (myScript.tier==1){Debug.Log ("jghfjgh");
Vector3 direction = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation (direction);
Instantiate (fire, transform.position, rotation);
damage=Random.Range (30,60);
public class magicuser : MonoBehaviour {
public magicspells myScript; //bored mormon //magicspells name of generic script
void Update(){
Debug.Log (myScript.strength );// got the value of strength but had to make it public first.
myScript.fireball ();
}}
Woot it works it actually works. Its aliveeeeee. So i attached each script to a empty GO and had them reference each other and it works like a charm( have to make everything public) finally an idea i had actually worked out in a reasonably short space of time:smile:
Thx for the assists guys not sure if this is a bad way to do this?? If it is someone correct me before i go deep down this path.
Isn’t that what i replied yesterday? Maybe you misunderstood it. Sorry then.
Anyway, as long as you keep it clean and don’t lose yourself in a chaotic structure it should be okay.
The only thing you may want to watch our for is that you do not try to access the other script’s variables too early, so only when you’re sure they have been initialized, otherwise you might encounter some errors or unwanted behaviour. But that’s rather a general rule which applies to many situations.
Yea i know about attaching a script to a GO but I didn’t think of reciprocating the action on the Go that the GO is attached to so it has a direct reference to the script as well which s how it would behave if i could just use the script without it attached to a GO so all’s good…