Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Send Vector3 of a Instanz to a another Script. Need some Help ...

Discussion in 'Scripting' started by Mr_Teels, Aug 14, 2015.

  1. Mr_Teels

    Mr_Teels

    Joined:
    Jul 14, 2015
    Posts:
    14
    Hey.

    I try to spawn a GameObjekt (Target). With the Target a UI Button Spawn. When this Button is pressed the Camera should jump to the position of the Target!

    I have a TargetController Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TargetController : MonoBehaviour {
    5.     private bool ButtonPressed;
    6.     public GameObject kamera;
    7.  
    8.  
    9.     void Update(){
    10.         kamera = GameObject.Find("Kamera");
    11.         if (ButtonPressed) {
    12.             kamera.SendMessage("ButtonPressed");
    13.         }
    14.      
    15.     }
    16.  
    17.     //UI activation
    18.     public void Button() {
    19.         ButtonPressed = true;
    20.     }
    21. }
    The UI Button triggers "public void Button". Then the script finds the Kamera GameObject and send a message.

    the "Camera Jump to Target" Part of the CameraController:
    Code (CSharp):
    1.         //Camera should Jump to Target.
    2.         if (CameraJumpTarget) {
    3.  
    4.             Vector3 TargetJumpXY = new Vector3(TargetPosition.x, TargetPosition.y, -50f);
    5.             transform.position = TargetJumpXY;
    6.  
    7.             if (transform.position == TargetJumpXY){
    8.                 CameraJumpTarget = false;
    9.             }
    10.  
    11.         }

    The Camera Jumps. But only to Position 0,0,0.

    I need to Send the Position of the GameObjekt "Target" to the Camera. Its a Prefab. So a lot of them will be arround later. If i let the Camera get the Position of the Target the Script will habe a lot of identical Targets!

    I need a Posibility to SEND the Target Position (Vector3) to the Camera.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Inside TargetController, use GetComponent<CameraController>() on your Kamera object to access the CameraController script and then assign it the intended target position.
     
  3. Mr_Teels

    Mr_Teels

    Joined:
    Jul 14, 2015
    Posts:
    14
    Wohh Thanks for your Help ^^
    After 2 Days it works.