- Home /
How do I create an Initial Spawn Point?
I am trying to get rid of an error that I am getting involving my character and camera. the first line of my code is the error. This is from my camera script
var Target = transform;
I saw a tutorial once a while back that said adding a spawn point will fix this error. But I can't find that anymore so I am asking the community to teach me a bit about how to make a spawn point.
There is nothing wrong with this like of code unless Target is the name of another script. We need to see your code in context, or at least a small sample of code that compiles and exhibits the error you are struggling with.
Ok this is the error I'm getting
InternalGetTransform can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.
This is my camera script
var Target = transform;
var distanceFromPlayer = 10;
function LateUpdate() { transform.position = Target.position + Vector3(distanceFromPlayer,0,0 ); }
when I double click the error it loads monodevelop and highlights the first line.
Answer by robertbu · Aug 02, 2013 at 03:36 AM
You can fix your error by breaking the assignment apart:
var Target;
Target = transform;
Or better yet:
var Target;
function Start() {
Target = transform;
}
But reading through your code, I don't believe either one is what you want to do. You want to do this:
var Target : Transform;
You can then drag and drop your player onto the 'Target' variable in the Inspector. Or you can find it:
function Start() {
Target = GameObject.Find("Player").transform;
}
Note: variables should be named with a lower case first letter. This is the convention used my most of the programmers on this list, and it makes it easier for them to read and debug.
Ok, I'll try that. Is the "Player" component based on the tag associated with my character or the actual name of the character?
Tried all 3 suggestions and none of them have worked thus far. I guess I'll have to experiment around a bit more to find the answer.
GameObject.Find() finds the name. If you want the tag, use GameObject.FindWithTag(). By not working do you mean it does not compile or does not do what you want?
Whenever my target variable = transform I get the error saying that I can't set that as a variable but if I try the other suggestions, it removes the variable from my inspector thus making me unable to set the target as my player. When I use the GameObject.Find code, the camera just zooms out very quickly and doesn't follow my character at all.
Based on your code, this is what I think you want:
#pragma strict
private var Target : Transform;
var distanceFromPlayer = 10;
function Start() {
Target = GameObject.Find("Player").transform;
}
function Update() {
transform.position = Target.position + Vector3(distanceFromPlayer,0,0 );
}
Note your "Player" game object must be named "Player" with an upper case 'P'.
Answer by ruinstarr · Aug 02, 2013 at 09:21 PM
Ok, so your script didn't work right for me either, but after some experimenting I did get it to work. Here is what I got
public var Player : Transform;
var distance = 10;
function Update(){
transform.position = Player.position + Vector3(distance, 0, 0);
}
This is a working, error free script for a 2D or 2.5D sidescroller. For anyone that needs a script for camera this one works flawlessly.