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

How do I set a Follow-Camera's target to a newly instantiated player?

Discussion in 'Scripting' started by schuette, May 24, 2013.

  1. schuette

    schuette

    Joined:
    Jan 6, 2013
    Posts:
    14
    I'm new to C# and my procedural programming insights are steering me wrong.

    If I create a new player, my simple FollowCamera.cs script, attached to the main camera, works fine, PROVIDED I manually drag the game object "player" onto the "target" slot of the script. I built these from demo's and tutorials.

    But what I need to do is instantiate a new player at run-time, when "drag and drop" is not available to the user.

    How do I do the equivalent "drag and drop" in C#, assuming I just made a new Game Object called "localplayer"?

    What does NOT work is something simple like:
    FollowCamera.target = localplayer;

    How should I do this?
    Thank you!
     
  2. TBranan

    TBranan

    Joined:
    May 22, 2013
    Posts:
    27
    Use:

    Code (csharp):
    1. GameObject _player;   //Assign this as your already in-game player
    2.  
    3. void Update()
    4. {
    5.        if(_player == null)   //If _player suddenly is lost
    6.        {
    7.                _player = GameObject.Find("Player");    //Search for instiated Player
    8.        }
    9.        else
    10.        {
    11.                Debug.Log("Player Is Alive!");   //Print player is alive.
    12.        }
    13. }
     
    unity_QxrabXrBPvhG7g likes this.
  3. schuette

    schuette

    Joined:
    Jan 6, 2013
    Posts:
    14
    Tyler, thank you for the speedy reply!

    Unfortunately I don't understand "Assign this as your already in-game player".

    How does creating yet one more GameObject tell my camera script to use it as a target? I already know that a GameObject called "localPlayer" is the desired target. So, yes, I could create another GameObject called _player and put in a command such as:

    _player = GameObject.Find("localPlayer");

    But how do I get that information passed into my FollowCamera.cs script as the desired instantiation of the "target" variable?

    I'm sure it's obvious to you when you say "assign" it, but that's the part of the whole thing I need spelled out, because it's what I do NOT understand how to do.
     
    Last edited: May 24, 2013
  4. TBranan

    TBranan

    Joined:
    May 22, 2013
    Posts:
    27
    I see, you already have a target selection variable in your FollowCamera. What I was doing in the other code was giving you an example of HOW to reassign the target variable to an instantiated object.

    Just insert this code, below, into the Void Update part of your code.


    Code (csharp):
    1. if(target == null)   //If player suddenly is lost
    2.        {
    3.                target = GameObject.Find("localPlayer") //looks for any instances (objects) of "localPlayer" in the scene
    4.  
    5.        }
    6.        else
    7.          {
    8.               Debug.Log("Player Is Alive!");   //Print player is alive.
    9.          }