- Home /
Inspector variable MonoScript, using for creating a List?
I have a script that checks through all gameObjects and adds the ones with a particular script to a list. It works if I use a script that is strictly defined in the Assets, but if I use the variable called script
, declared as var script : MonoScript;
, it throws the error "The name 'script' does not denote a valid type ('not found'). Did you mean 'UnityEditor.ScriptCallOptimizationLevel'?".
Code:
import System.Collections.Generic;
function Start () {
}
var obj : GameObject;
var script : MonoScript;
var arrayIndex = 0;
var test : List.<script> = new List.<script>();
function Awake() {
script = script;
for(var t : script in GameObject.FindObjectsOfType(script))
{
test.Add(t);
}
}
function Update () {
var lineRenderer : LineRenderer = GetComponent(LineRenderer);
lineRenderer.useWorldSpace = false;
lineRenderer.SetVertexCount(2);
var hit : RaycastHit;
Physics.Raycast(transform.position, transform.forward, hit);
if(hit.collider){
lineRenderer.SetPosition(1,Vector3(0, 0, hit.distance));
if(hit.collider.tag == "Player") {
test[arrayIndex].condition = true;
Debug.Log("Hit laser. ");
}
}
else{
lineRenderer.SetPosition(1,Vector3(0, 0, 5000));
}
}
@script RequireComponent(LineRenderer)
First, there's no such thing as a $$anonymous$$onoScript
(unless that's a type defined by you) - There's something called a $$anonymous$$onoBehaviour
Second, if I got you right, you want the type that you're adding to the list, to be dynamic? (you want to specify via the inspector, which type that you want to use?)
[EDIT]: There is actually a `$$anonymous$$onoScript`, see Jamora's comment below.
No such thing as a $$anonymous$$onoScript? Really? The syntax highlights it and it works perfectly other than the list, so I should have reason to believe it does. Yes, that is what I meant. And sorry that I'm unclear a lot, I'm new to JS within Unity and just getting used to the terms.
Can I ask why would you want to do that? (BTW the syntax here highlights any type, even if it doesn't exist, nothing gets compiled here for it to tell you that there's no such thing as $$anonymous$$onoState
^^)
I'm probably not doing it right, but; I'm doing this so that this trigger script (once working, it will not be attached to this laser and ins$$anonymous$$d any gameObject, not the concern yet) can reference and script in any object, so that it may tell a cube to move or another one to rotate, etc. Just widening the usage. But there's probably a better way, yes? And if $$anonymous$$onoScript doesn't exist, why does the inspector recognize it and let me pick a script then?/: And if I type simply "script" or "$$anonymous$$ono", "$$anonymous$$onoScript" is a suggested autofill. And no, it doesn't highlight $$anonymous$$onoState
. (I'm talking about the highlighting in the Unity IDE)
$$anonymous$$onoScript is an internal asset representation of any script compilable by Unity. Unless you're making your own script assets with the Editor, you should not use $$anonymous$$onoScript.
$$anonymous$$onoBehaviour is the script that all user-made scripts attachable to GameObjects extend, as explained by vexe in his answer.
Answer by vexe · Oct 10, 2013 at 05:48 AM
Since any script you attach to your gameObjects inherits from MonoBehaviour
, you have to use a public variable of type MonoBehaviour
and then you could call GetType()
to get the explicit type.
var script : MonoBehaviour;
private var arr : MonoBehaviour[]; // no need for the `test` list
function Awake()
{
arr = GameObject.FindObjectsOfType(script.GetType()) as MonoBehaviour[];
if (arr == null || arr.Length == 0)
print ("No objects of type " + script.GetType() + " was found");
}
Now, let's say you have a OtherScript
script, you could assign it to the script
variable via the inspector, and arr
will try to find and store all OtherScript
s in your scene.
But notice, since you're doing it this way, this requires that you cast it down to something very specific since you're now storing MonoBehaviours
- In other words, if you assigned OtherScript
to script
and now arr
has reference to all OtherScript
s, in order to, for example access something from your OtherScript
(like in your case the condition
variable) - you'd have to do (arr[INDEX] as OtherScript).condition
But be careful, cause that cast might fail (return null), so you have to make sure it's not null first:
var myOther = arr[INDEX] as OtherScript;
if (myOther != null) // only then
myOther.condition = true;
Yes, I already told you. arr
is now storing $$anonymous$$onoBehaviour
s, you must cast it down to the type (script) that has the condition
variable. (Just like I showed you)
var myVar = arr[arrayIndex] as INSERT_YOUR_TYPE_HERE;
if (myVar != null)
myVar.condition = true;
Okay, good to know, thanks. I'm currently at school, ironically in my program$$anonymous$$g class with everything done, so I don't have access to the project. I would prefer to work with C# as I have experience in Java, but the other member of our $$anonymous$$m is using JS, so I'm kind of stuck rolling with it. I don't have enough reputation to upvote comments.
The only way that would throw a NullRefExc
is if script
wasn't assigned. $$anonymous$$ake sure you assign script from the inspector. And just to make sure:
function Awake() {
if (script == null)
print ("Script value is not assigned, you will get a NullRefExc if you continue");
arr = GameObject.FindObjectsOfType(script.GetType()) as $$anonymous$$onoBehaviour[];
if (arr == null || arr.Length == 0)
print ("No objects of type " + script.GetType() + " was found");
}
About your renderer:
@script RequireComponent(LineRendered); // <-- fix typo (LineRendered) -> (LineRenderer)
Of course it does. $$anonymous$$onoScript
is something else see @Jamora's comment on your question.
When you assign script
, you have to assign it something, that currently exist in your game, so if you wanted to assign it a TransformObject
, you have drag-drop to script
a gameObject that has TransformObject
on it.
Did the printing statement that I put execute?
Friend, you must understand something.
In order for your idea to work, script
must be assigned. In order for you to assign script
, you must have a gameObject
in your scene, that has a $$anonymous$$onoBehaviour
attached to it (a script). You then drag-drop that gameObject
to script
, then script
will take the $$anonymous$$onoBehaviour
that's attached to it.
In my example, I have a cube which has a temp
script ($$anonymous$$onoBehaviour) attached to it, it has your code. $$anonymous$$y camera has a temp2
script attached to it, so I drag-drop my camera to the script
variable, that's in my cube's temp
component. It's true that I'm drag-dropping the camera, but that doesn't mean that script
is now referring to the camera, script
now refers to temp2
which is as I mentioned, a $$anonymous$$onoBehaviour
attached to the camera.
If you don't do something similar, it won't work.
Answer by Tomer-Barkan · Oct 10, 2013 at 05:40 AM
If you want to get all the scripts of a specific type, you have to pass the type of the script to GameObject.FindObjectsOfType()
.
So if script
variable is an instance of such script, and you want to get all other instances of that script type, you'd need to pass the type of the script variable to the method.
You can do that with the following code:
var allScripts = GameObject.FindObjectsOfType(typeof script);
I am trying to create a List with a $$anonymous$$onoScript
variable, and is it possible. I don't see how this is relevant or the answer to my question, excuse me if I'm wrong...
allScripts will contain the list of what you're looking for after you run the command above.
It won't be of type $$anonymous$$onoScript though, it will be of the type of script that you were looking for, or you can make it $$anonymous$$onoBehaviour[] if you want:
var allScripts: $$anonymous$$onoBehavior[] = GameObject.FindObjectsOfType(typeof script);
Your answer
Follow this Question
Related Questions
How to find the opposite variables list? 1 Answer
Shotgun using array/list in Javascript 1 Answer
How to put buttons in a list? 1 Answer
Storing functions in a list/array? 1 Answer
A node in a childnode? 1 Answer