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

Problem with camera follow

Discussion in 'Scripting' started by Crazyman582, Sep 30, 2015.

  1. Crazyman582

    Crazyman582

    Joined:
    Aug 13, 2015
    Posts:
    13
    Hi. I'm trying to make a camera follow script (2d topdown) that will get my character automatically since my game is proceduraly generated. I have been using GameObject.Find but my problem is for this I need my variable to be a GameObject, but for the rest of the script it needs to be a Position.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class camerafollow : MonoBehaviour {
    5.  
    6.     public GameObject myTarget;
    7.    
    8.     //Start is called on load
    9.     void Start() {
    10.         myTarget = GameObject.Find("paladin-inventory(Clone");
    11.    
    12.     }
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (myTarget != null) {
    16.        
    17.         Vector3 targPos = myTarget.position;
    18.         targPos.z = transform.position.z;
    19.         transform.position = targPos;
    20.            
    21.         }
    22.  
    New to coding and this is giving me bit of a problem
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    on line 17, instead of "myTarget.position;" try "myTarget.transform.position;"
     
  3. Crazyman582

    Crazyman582

    Joined:
    Aug 13, 2015
    Posts:
    13
    Thanks for the reply, this fixed the error but still does not cause camera to follow. Help is appreciated (although that was very helpful)
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    That looks like it should follow the player on the x and y axis... are you sure "myTarget" isn't always null? The "Start()" function only ever gets called once just before the first time "Update()" is called, so if it's unable to find the player then, it will never find the player.
     
  5. Huknar

    Huknar

    Joined:
    Mar 6, 2015
    Posts:
    40
    Make sure that "myTarget" is being correctly found, otherwise your code will not run.

    If myTarget exists in the editor, it should be finding it with the correct name applied, if it's generated through a script it sounds like it might be a script execution order issue. (That it gets generated after your code tries to find it.)

    I'm noticing that "paladin-inventory(Clone" is missing a bracket inside the string, I don't know what you have your names set up as, but it seems likely that typo is causing a name mismatch. :)