Question by
RVDL_IT · May 03, 2017 at 04:13 PM ·
c#errorgameobjectsfindgameobjectswithtagcs1061
GameObject[] doesn't have a definition for transform/name/etc...
I have a pick up script that has GameObject[] for the code FindGameObjectsWithTag.
This is my script:
using UnityEngine;
using System.Collections;
using CameraRayCastLine;
using PickUpSphereCast;
namespace CarryingItems {
public class PickUp : MonoBehaviour {
// The variables for the positions.
// Forward drop offset.
public float OffsetDropPosition = 2;
// The boolean that checks if the camera's RayCast is hit.
public bool CameraRayHit;
// The Player and the tagged objects.
public GameObject Camera;
public GameObject[] CarryableObject;
public GameObject Character;
public Transform CharacterTransform;
// Is an item picked up at the moment?
public bool ItemPickedUp;
// Are you in the item's SphereCast?
public bool InItemSpherecast;
// Are you looking at an item?
public bool LookingAtObject;
void Awake() {
// The origin of the objects.
Camera = GameObject.Find("MainCamera");
CarryableObject = GameObject.FindGameObjectsWithTag("CarryableObjects");
Character = GameObject.Find("Character");
CharacterTransform = GameObject.Find("Character").transform;
// The code that references the RayCast script of the camera.
CameraRayHit = Camera.GetComponent<CameraRayCastLine.PickUpRayCast>().CameraRayCast;
// The code that references the SphereCast of another script.
InItemSpherecast = PickUpSphereCast.ItemPickUpSphereCast.ItemSphereCast;
}
void Start() {
// The code that tells you how to pick up an object through the debug log.
Debug.Log("Press E to pick up an object.");
}
void Update() {
}
void PickUpItemCode() {
// If another item isn't picked up at the moment.
if(ItemPickedUp == false) {
Debug.Log("No items picked up.");
// If you're in the SphereCast.
if(InItemSpherecast == true) {
Debug.Log("Spherecast = true.");
// If you're looking at the item.
if (CameraRayHit == true) {
// A boolean for the GUI.
LookingAtObject = true;
// A code that says you can pick up the item in question.
Debug.Log("Press E to pick up the" + CarryableObject.name + ".");
// If you press the E key.
if (Input.GetKeyDown("e")) {
// The position relative to the character.
CarryableObject.transform.position = new Vector3(Character.transform.position.x + 1, Character.transform.position.y + 1, Character.transform.position.z + 1);
// The rotation relative to the character.
CarryableObject.transform.rotation = Character.transform.rotation;
// The gravity.
CarryableObject.GetComponent<Rigidbody>().useGravity = false;
// A message showing that the object was picked up.
Debug.Log("You picked up the " + CarryableObject.name + "! Good for you!");
Debug.Log("Press the left mouse button to use an object.");
Debug.Log("Press E to drop an object.");
// The code that acitvates the boolean that is needed for the dropping script.
ItemPickedUp = true;
// The code that makes the chracter the new objects parent.
CarryableObject.transform.SetParent(CharacterTransform);
}
}
else {
// You're not looking at an object.
LookingAtObject = false;
}
}
}
else {
// The code that makes sure that the pick up boolean is not active when no item is picked up.
ItemPickedUp = false;
}
}
void DropItemCode() {
// If you're holding an item.
if(ItemPickedUp == true){
// If you press the E key.
if(Input.GetKeyDown("e")){
// The drop position.
CarryableObject.transform.position = new Vector3(CarryableObject.transform.position.x, CarryableObject.transform.position.y, CarryableObject.transform.position.z + OffsetDropPosition);
// The gravity gets reenabled.
CarryableObject.GetComponent<Rigidbody>().useGravity = true;
}
}
}
void ONGUI() {
if(LookingAtObject == true){
// The how to pick up items text.
GUI.Label(new Rect(500, 200, 100, 20), "Press E to pick up the" + CarryableObject + ".");
}
}
}
}
Comment
Answer by jdean300 · May 03, 2017 at 04:21 PM
GameObject[] is a whole set of objects. What you need to do is choose which one you want to access, something like this: CarryableObject[0].transform.position
.
I want which gameobject is picked to be relevant to which one I'm trying to pick up. How do I do this using GameObject[]?