- Home /
Instantiated object not appearing.
Hello. I'm to trying to create a very basic minecraft-esque game (which I'm starting to think of as the "Hello world" program of Unity) purely to further my understanding of C#. However, I've run into a problem. I'm trying to implement a feature where a faded out block appears that shows you where your block is going to be placed. I'm attempting to do this by instantiating a collider-less faded block, and then deleting it when you're cursor moves away from that point. Unfortunately, before I've even really started, I've run into the problem that the block doesn't instantiate at all. Code:
using UnityEngine;
using System.Collections;
public class Interaction : MonoBehaviour {
RaycastHit rayHit;
public Transform blockFaded;
private bool hasLooked;
void Start () {
hasLooked = false;
}
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection (Vector3.forward);
Vector3 spot = new Vector3(Mathf.Round (rayHit.point.x), Mathf.Ceil (rayHit.point.y),Mathf.Round (rayHit.point.z));
if(Physics.Raycast (transform.position, fwd, out rayHit, 5))
{
if (hasLooked==false)
{
Instantiate (blockFaded, spot, Quaternion.identity);
Debug.Log ("hit");
hasLooked = true;
}
}
}
}
Attached to a first person controller, this script should just place a single faded block on the ground and that's it, but the block is never created. The Debug.Log, however, runs perfectly when it's supposed to. Anybody know what's going on?
Answer by Tomer-Barkan · Dec 01, 2013 at 05:17 AM
You're setting spot:
Vector3 spot = new Vector3(Mathf.Round (rayHit.point.x), Mathf.Ceil(rayHit.point.y),Mathf.Round (rayHit.point.z));
And you're using rayHit, but this command is before you run the RayCast, so rayHit is still not populated with the raycast hit values... Move this command inside the if.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123141im_/https://answers.unity.com/themes/thub/images/avi.jpg)