- Home /
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)
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.
oh and yes, I double checked there is a $$anonymous$$ainCamera attached to Player...its got me stumped...
What do your console logs show for playerobject and playercamera?
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)
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;
}
}
}
Your answer
Follow this Question
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