C# changing the Parent of the camera and Tags

Hi guys,
I’m a complete noob, if you could reply in such a way that an idiot could understand it that would be much appreciated, ty.
So here’s the deal. I have a script that spawns your character at the start of the game. All I basically want is parent the Main Camera to that character when the game starts.
I first tried using a Parent script but that proved difficult since firstly there are actually a number of different characters that can spawn at the start of the game which are all different prefabs with different names that can be instantiated by this script. So in the script that means I would need to somehow include all the different prefabs in a big IF statement.
The other problem is that when one is instantiated it gets “(Clone)” added to the end of its name, and I can’t figure out how to not get the script confused about those brackets in the name lol

Here is the script that I’m talking about

using UnityEngine;
using System.Collections;

public class AddCar : MonoBehaviour {
   
    //array of cars visible in the inspector
    public GameObject[] drivableCars;

    void Start(){
    //instantiate the selected car on the right position
    var pos = new Vector3(10, 0, 0);
    Instantiate(drivableCars[PlayerPrefs.GetInt("selectedCar")], pos, transform.rotation);
    }
}

So then I thought; wouldn’t it be smarter if the script that instantiates a character at the start of the game could also give it a Tag which I could then use to parent the camera on.
So I tried adding

       var drivableCars = Instantiate(ExampleTwo, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;

which I found on a forum. But I have no idea how to use this.

thx in advance guys ^^

One way to handle this would be:

//Instaniate the player car
GameObject player = (GameObject)Instantiate(drivableCars[PlayerPrefs.GetInt("selectedCar")], pos, transform.rotation);

//Set the main camera as a child of the player
Camera.main.transform.SetParent(player.transform);
2 Likes

thx for the reply Intense_Gamer94

I pasted that in there as it is and it just works. I don’t understand at all how but it does xD
thx
So you’re not using Tags right? What does “player” refer to? Also “Camera.main.transform”, I kept trying “Main_Camera.transform” how does one know these things? lol

One more thing I’ve been working on for over an hour now but can’t figure out. I want the camera to get parented after a delay. A timer would be nice. It gives me errors though if I use

    void Start(){
        var pos = new Vector3(10, 0, 0);
    GameObject player = (GameObject)Instantiate(drivableCars[PlayerPrefs.GetInt("selectedCar")], pos, transform.rotation);

       yield return new WaitForSeconds(5);

            Camera.main.transform.SetParent(player.transform);
    }

something about
The body of ‘AddCar.Start()’ cannot be an iterator block because ‘void’ is not an iterator interface type Traffic(3d).CSharp

I’m guessing it doesn’t like “Void Start” in combination with “yield return” for some reason. I was also playing around with an X position trigger but that doesn’t seem to work since the X position of the character is a constant 0 for some reason.
The next thing I would try is to just place a trigger zone which in my mind would definitely work. But I wanna see how well a timer would fare. thx
You’re Awesome ^^

Yield statement only works in a coroutine. You could do this to delay the start:

void Start ()
{
    StartCoroutine (Timer ());
}

IEnumerator Timer ()
{
    yield return new WaitForSeconds (0.5f);
    DoSomethingElse (); // delayed stuff
}

thx MD_Reptile

I replaced “void Start ()” with “IEnumerator Start ()” and that seems to work. It might cause me other problems down the line though xD
I would like to fit your solution in here somehow, it seems less intrusive. No idea how though. I tried a few different combinations now.
It looks like this at the moment.

    void Start() {
        var pos = new Vector3(10, 0, 0);

        GameObject player = (GameObject)Instantiate(drivableCars[PlayerPrefs.GetInt("selectedCar")], pos, transform.rotation);
        {
            StartCoroutine(Timer());
        }

        IEnumerator Timer ()
        {
            yield return new WaitForSeconds(0.5f);
        }
        Camera.main.transform.SetParent(player.transform);

    }

The “IEnumerator” part cannot be inside a “void” method (or another coroutine method) and must be outside the scope of the start method. Like I showed in the example.

Mmmmm but then Camera.main.transform.SetParent(player.transform); is outside the void Start()
how would I still put that in afterwards?
I mean I don’t want to start a void update(). Then it would reparent the Camera every frame.

Sorry for the noob question.

    NON WORKING CODE

”The Name ‘player’ does not exist in the current context"
It sais that for the last line “Camera.main.transform.SetParent(player.transform);”
When I put “private object player;” before the void start it’s fixed but then the ‘transform’ at the end gets a bug.

thx so much for the help so far. This is fun ^^

Haha ok, my bad, I didn’t test the code, but I think this should work:

    private GameObject player;

    void Start()
    {
        var pos = new Vector3(10, 0, 0);
        player = (GameObject)Instantiate(drivableCars[PlayerPrefs.GetInt("selectedCar")], pos, Quaternion.identity);
        StartCoroutine(Timer()); // call to start the coroutine
    }
   
    IEnumerator Timer()
    {
        yield return new WaitForSeconds(0.5f); // wait half a second
        Camera.main.transform.SetParent(player.transform); // then this happens.
    }

yay it worx
thx MD_Reptile ^^

your patience towards noobism is impeccable

1 Like

Haha, no problem, good luck!