- Home /
Question by
Camkash · Aug 31, 2014 at 03:19 PM ·
c#instantiate
Instantiate object ahead of player
It's been asked before, but I'm new to C# and Unity. Used to matlab. Basically the game runs, but no object instantiates when I click.
using UnityEngine;
using System.Collections;
public class Building : MonoBehaviour {
public GameObject sphere1=null;
// Use this for initialization
void Start () {
sphere1=Instantiate(Resources.Load("Prefabs/prefab_sphere")) as GameObject;
}
// Update is called once per frame
void Update () {
var playerObject = GameObject.Find("Player");
var playerPos = (Vector3)playerObject.transform.position;
var buildingPos = playerPos + 10*Vector3.forward;
Debug.Log(playerPos);
if(Input.GetMouseButtonDown(0))
sphere1=Instantiate(Resources.Load("Prefabs/prefab_sphere"),buildingPos,Quaternion.identity) as GameObject;
}
}
Comment
I solved this by rena$$anonymous$$g "First Person Controller" to "Player".
Well... you were calling the player with this:
var playerObject = GameObject.Find("Player");
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player instantiates Backwords 0 Answers
Instantiate prefab once help C# 1 Answer
Distribute terrain in zones 3 Answers
How do you Change Direction of a Shot towards Mouse Position? (Top Down Shooter) 1 Answer