Unity3D – Convert from another GameObject script

I am trying to create a script to set the object upon instantiation. The problem is, I am not clear how to do this. I have this function..

function spawnPlayer()
{
var CameraScript = GameObject.Find(PlayerPrefab.name).GetComponent("Camera Control");
Network.Instantiate (PlayerPrefab, spawnObject.position, Quaternion.identity, 0);
)

PlayerPrefab will be the prefab that will be instantiated. When this happens, I need to be on another GameObject Set the instantiated gameObject. This GameObject is a script named “Camera Control”, which has a transformation Target I want to set. How can I do this?

The code you posted is incorrect. You are using the name of PlayerPrefab to find the Camera connected to the camera Control script? Go through this logic, and then at the moment PlayerPrefab is instantiated, in the second line, you will have a second camera.

I think what you want to do is: instantiate the player prefab And let the camera point to the player.

So I assume that the CameraControl script has been created. Before you start coding, you need the following.

>Attach the CameraControl script to the camera in the scene.

>Make sure that the Player script is attached to the Player Prefab.

>There is a third script that will instantiate the PlayerPrefab. I will call it an Instantiator. Attach it to the empty space in the scene GameObject, treat it as a World GameObject. We call it a world.
>Make sure that the Instantiator script is attached to the World GameObject and it points to the PlayerPrefab.

Code: Instantiator

The Instantiator script will generate and create the content we will use in the scene.

#pragma strict

var PlayerPrefab: GameObject;
< br />function Start ()
{
// You can add position and rotation to the function call if you like.
var p = Instantiate(PlayerPrefab) as GameObject;

// Find the camera script and point to Player's transform.
Camera.main.GetComponent("CameraControl").SendMessage("setTarget", p.transform);
}

Pay attention to the fact that I used the MainCamera in the scene to mark it for you, so it is easy to find.

Code: CameraControl

CameraControl will have a follower player that you think is appropriate Logic. Please note that the target will point to what the camera will focus on. Of course, you have to write around the player.

var target: Transform;

function setTarget(t: Transform)
{
target = t;
}

I just taught myself a little JavaScript. I have never used it before.

I am trying to create a script to set the object at instantiation The problem is that I don’t know how to do this. I have this function..

function spawnPlayer()
{
var CameraScript = GameObject.Find(PlayerPrefab.name).GetComponent("Camera Control");
Network.Instantiate(PlayerPrefab, spawnObject.position, Quaternion.identity, 0);
}

< p>PlayerPrefab will become the prefab that will be instantiated. When this happens, I need to set the instantiated gameObject on another GameObject. This GameObject is a script called “Camera Control”, which contains a The transformation Target to be set. How should this be done?

The code you posted is incorrect. Are you using the name of PlayerPrefab to find the Camera Control script connected to the camera? Go through this logic, and then at the moment PlayerPrefab is instantiated, in the second line, you will have a second camera.

I think what you want to do is: instantiate the player prefab And let the camera point to the player.

So I assume that the CameraControl script has been created. Before you start coding, you need the following.

>Attach the CameraControl script to the camera in the scene.

>Make sure that the Player script is attached to the Player Prefab.

>There is a third script that will instantiate the PlayerPrefab. I will call it an Instantiator. Attach it to the empty space in the scene GameObject, treat it as a World GameObject. We call it a world.
>Make sure that the Instantiator script is attached to the World GameObject and it points to the PlayerPrefab.

Code: Instantiator

The Instantiator script will generate and create the content we will use in the scene.

#pragma strict

var PlayerPrefab: GameObject;
< br />function Start ()
{
// You can add position and rotation to the function call if you like.
var p = Instantiate(PlayerPrefab) as GameObject;

// Find the camera script and point to Player's transform.
Camera.main.GetComponent("CameraControl").SendMessage("setTarget", p.transform);
}

Pay attention to the fact that I used the MainCamera in the scene to mark it for you, so it is easy to find.

Code: CameraControl

CameraControl will have a follow-up player you think is appropriate Please note that the target will point to what the camera will focus on. Of course, you must write around the player.

var target: Transform;

function setTarget(t: Transform)
{
target = t;
}

I just taught myself A bit of JavaScript. I have never used it before.

Leave a Comment

Your email address will not be published.