Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IHazABone · Oct 10, 2013 at 05:13 AM · javascriptarraylist

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)
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Oct 10, 2013 at 05:19 AM 0
Share

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.

avatar image IHazABone · Oct 10, 2013 at 05:22 AM 0
Share

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.

avatar image vexe · Oct 10, 2013 at 05:30 AM 0
Share

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 ^^)

avatar image IHazABone · Oct 10, 2013 at 05:32 AM 0
Share

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)

avatar image Jamora · Oct 10, 2013 at 03:16 PM 1
Share

$$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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best 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 OtherScripts 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 OtherScripts, 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;
Comment
Add comment · Show 39 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Oct 10, 2013 at 06:03 AM 1
Share

Yes, I already told you. arr is now storing $$anonymous$$onoBehaviours, 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;
avatar image IHazABone · Oct 10, 2013 at 05:18 PM 1
Share

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.

avatar image vexe · Oct 11, 2013 at 04:13 AM 1
Share

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)
avatar image vexe · Oct 11, 2013 at 04:20 AM 1
Share

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?

avatar image vexe · Oct 11, 2013 at 04:51 AM 1
Share

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.

alt text

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.

howto.png (70.0 kB)
Show more comments
avatar image
0

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IHazABone · Oct 10, 2013 at 05:45 AM 0
Share

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...

avatar image Tomer-Barkan · Oct 10, 2013 at 05:57 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges