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 ravendevil67 · Apr 04, 2013 at 01:58 AM · arrayfind

How do I make an array and then access an Object from the array

here is the code that doesnt seem to work...

 #pragma strict
 #pragma implicit
 #pragma downcast
  
 var controllableObjects     : GameObject[];
  
 var playerObject         : GameObject;
 var playerCamTrans       : Transform;
 var playerCamera         : GameObject;
  
 var targetObject         : GameObject;
 var targetCamera         : GameObject;
  
 var canUse   = true;
 var rayDepth    = 3;
  
 // Reticle
 var drawReticle = true;
 var crosshairTexture    : Texture;
 private var position    : Rect;
  
 private var objCamera   : GameObject;
  
 class ControlObjectManager extends MonoBehaviour
 {
     function Start ()
     {
        Debug.Log("Start  : ");
        // Add the 'Player'
        playerObject  = GameObject.FindGameObjectWithTag("Player");
        Debug.Log("playerObject            : " + playerObject);
  
        playerCamera  = playerObject.transform.FindChild("MainCamera").gameObject;
        Debug.Log("playerCamera  : " + playerCamera);
  
        controllableObjects   = GameObject.FindGameObjectsWithTag("ControllableObject");
  
         // iterate through the array
         for (var obj in controllableObjects)
         {
             if (obj.tag == "ControllableObject")
             {
           objCamera = obj.transform.FindChild("MainCamera").gameObject;
           Debug.Log("Turning Off Camera  : " + objCamera);
           objCamera.camera.depth = 1;
                objCamera.camera.active = false;
             }
         }
  
        // Reticle
        position = Rect((Screen.width - crosshairTexture.width) / 2,
                    (Screen.height - crosshairTexture.height) / 2,
                     crosshairTexture.width, crosshairTexture.height);
  
        // Mouse Cursor
         Screen.lockCursor = true;
     }
  
     function Update ()
     {
        if (canUse && Input.GetButtonDown("Use"))
        {
          Debug.Log("Use Activated !");
  
          if (!targetObject)
          {
           var camX    = playerCamera.camera.GetScreenWidth() / 2;
           var camY    = playerCamera.camera.GetScreenHeight()    / 2;
  
           Debug.Log("playerObject    : " + playerObject);
           Debug.Log("From Camera     : " + playerCamera + "  ... Attached to GameObject  : " + playerCamera.transform.root);
  
           var hit: RaycastHit;
           var ray = playerCamera.camera.ScreenPointToRay (Vector3(camX, camY, rayDepth));
  
                if (Physics.Raycast(ray, hit) && hit.collider != null && hit.collider.gameObject.tag == "ControlTrigger")
           {
               Debug.Log("GameObject      : " + hit.collider.gameObject);
  
               playerObject.SendMessage ("SetControllable", false);
               playerCamera.camera.active = false;
               //playerCamera.camera.depth = 1;
  
               targetObject = hit.collider.gameObject.GetComponent(ControlTrigger).controlObject;
               Debug.Log("Target Object      : " + targetObject);
               targetObject.SendMessage ("SetControllable", true);
  
               targetCamera = targetObject.transform.FindChild("MainCamera").gameObject;
               Debug.Log("Target Camera      : " + targetCamera);
               targetCamera.camera.active = true;
               //targetCamera.camera.depth = 0;
           }
           else
           {
               Debug.Log("GameObject      : " + hit.collider.gameObject);
               targetObject = null;
           }
          }
          else
          {
           playerObject.SendMessage ("SetControllable", true);
           playerCamera.camera.active = true;
           //playerCamera.camera.depth = 0;
  
           targetObject.SendMessage ("SetControllable", false);
  
           //targetCamera.camera.depth = 1;
           targetCamera.camera.active = false;
          }
        }
     }
  
     function OnGUI()
     {
        // Draw Reticle
        if(drawReticle)
        {
          GUI.DrawTexture(position, crosshairTexture);
        }
     }
 }
 
 

error :

NullReferenceException UnityEngine.Component.get_gameObject () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:170) ControlObjectManager.Start () (at Assets/Assets/Scripts/ControlManager/ControlObjectManager.js:34)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Apr 04, 2013 at 02:15 AM

It looks like you're doing everything right as far as the code goes. You just have a null reference, so that means that something is not being found. You seem to know where your errors are, so that should narrow down your problem quite a bit.

If I were debugging this, I'd first make sure that there is a GameObject called MainCamera childed to "Player".

But since I'm not, I'd also suggest creating a public field in your script for you to just drag the camera onto the inspector, like so:

     public Camera playerCamera;

Now checkout the inspector for your Player, and you should see the field. Drag in the camera, and test it.

For your "ControllableObjects" problem, you probably can't just drag in a bunch of references, because, I assume, they're always changing. Again, it seems like the problem is that your gameObject doesn't have a child named "MainCamera". There really isn't anything else in your code to go wrong at that point.

Comment
Add comment · Show 4 · 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 ravendevil67 · Apr 04, 2013 at 03:21 AM 0
Share

added the rest of script....

avatar image ravendevil67 · Apr 04, 2013 at 03:45 AM 0
Share

oh and yes, I double checked there is a $$anonymous$$ainCamera attached to Player...its got me stumped...

avatar image iwaldrop · Apr 04, 2013 at 04:49 AM 0
Share

What do your console logs show for playerobject and playercamera?

avatar image ravendevil67 · Apr 04, 2013 at 05:23 AM 0
Share

playerObject comes up correct but the playerCamera is null

Start : UnityEngine.Debug:Log(Object) ControlObject$$anonymous$$anager:Start() (at Assets/Assets/Scripts/Control$$anonymous$$anager/ControlObject$$anonymous$$anager.js:27)

playerObject : First Person Controller (UnityEngine.GameObject) UnityEngine.Debug:Log(Object) ControlObject$$anonymous$$anager:Start() (at Assets/Assets/Scripts/Control$$anonymous$$anager/ControlObject$$anonymous$$anager.js:30)

NullReferenceException ControlObject$$anonymous$$anager.Start () (at Assets/Assets/Scripts/Control$$anonymous$$anager/ControlObject$$anonymous$$anager.js:32)

avatar image
0

Answer by ravendevil67 · Apr 05, 2013 at 02:31 AM

Nevermind, I fixed it. Thank you though...

 var canUse                        : boolean    = true;
 private    var controllableObjects    : GameObject[];
 private    var playerObject        : GameObject;
 private    var playerCamera        : GameObject;
 private    var targetObject        : GameObject;
 private    var targetCamera        : GameObject;
 var rayDepth                    : float        = 3;
 
 class ControlObjectManager extends MonoBehaviour
 {
     function Start ()
     {
         // Add the 'Player'
         playerObject = GameObject.FindGameObjectWithTag("Player").gameObject;
         playerCamera = GetChildByTag(playerObject, "Camera", "MainCamera");
         
         controllableObjects   = GameObject.FindGameObjectsWithTag("ControllableObject");
         
         // iterate through the array
         for (var obj in controllableObjects)
         {
             if (obj.tag == "ControllableObject")
             {
                 obj.SendMessage ("SetControllable", false);
                 var objCamera : GameObject = GetChildByTag(obj, "Camera", "MainCamera");
                 
                 if (objCamera)
                 {
                     objCamera.camera.depth = 1;
                     objCamera.camera.active = false;
                 }
                 else Debug.Log("No Camera Attached To : " + obj + " : Tagged : " + "MainCamera");
             }
         }
     }
     
     function Update ()
     {
         if (canUse && Input.GetButtonDown("Use"))
         {
             //Debug.Log("Use Activated !");
             
             if (!targetObject)
             {
                 var camX    = playerCamera.camera.GetScreenWidth()    / 2;
                 var camY    = playerCamera.camera.GetScreenHeight()    / 2;
                 
                 var hit: RaycastHit;
                 var ray = playerCamera.camera.ScreenPointToRay (Vector3(camX, camY, rayDepth));
                 
                 if (Physics.Raycast(ray, hit) && hit.collider != null && hit.collider.gameObject.tag == "ControlTrigger")
                 {
                     playerObject.SendMessage ("SetControllable", false);
                     playerCamera.camera.active = false;
                     playerCamera.camera.depth = 1;
                     
                     targetObject = hit.collider.gameObject.GetComponent(ControlTrigger).controlObject;
                     targetObject.SendMessage ("SetControllable", true);
                     
                     targetCamera = GetChildByTag(targetObject, "Camera", "MainCamera");
                     targetCamera.camera.active = true;
                     targetCamera.camera.depth = 0;
                 }
                 else
                 {
                     targetObject = null;
                 }
             }
             else
             {
                 playerObject.SendMessage ("SetControllable", true);
                 playerCamera.camera.active = true;
                 playerCamera.camera.depth = 0;
                 
                 targetObject.SendMessage ("SetControllable", false);
                 
                 targetCamera.camera.depth = 1;
                 targetCamera.camera.active = false;
                 targetObject = null;
             }
         }
     }
     
     function GetChildByTag(searchObj:GameObject, withType:String, withTag:String)
     {
         switch (withType)
         {
             case "GameObject" :
                 var childObjects = searchObj.transform.GetComponentsInChildren(GameObject, true);
                 
                 // iterate through the array
                 for (var obj in childObjects)
                 {
                     if (obj.gameObject.tag == withTag) return obj.gameObject;
                 }
             break;
         
             
             case "Camera" :
                 var childCameras = searchObj.transform.GetComponentsInChildren(Camera, true);
                 
                 // iterate through the array
                 for (var cam in childCameras)
                 {
                     if (cam.gameObject.tag == withTag) return cam.gameObject;
                 }
             break;
         }
     }
 }
 
 
Comment
Add comment · 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

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

11 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

Related Questions

How do I instantiate same tag/name GameObjects with an array 1 Answer

Find all audioSources check if they are playing 1 Answer

Get object child list with tag 2 Answers

FindWithTag to select Array and then parsing array 1 Answer

Need an advise, how to find all objects with a specific name and a specific script? 2 Answers


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