- Home /
getting raycast working on prefabs made from resources created at runtime
Hi Im having some problems with created (prefab) gameObjects and a raycast. I've created some cubes in the game with BoxColliders and using:
if (Physics.Raycast (ray, hit, 100.0)) {
distanceToGround = hit.distance;
Debug.DrawLine (ray.origin, hit.point);
}
to show me this in the editor. This works fine. I then created a new gameObject:
var myNewObj = Instantiate(newObject,Vector3.zero,Quaternion.identity);
where newObject is a prefab created beforehand from a resource. What do I have to do to get the raycast working on this object? a box collider would do. I tried:
var myNewObjRigbody = myNewObj.gameObject.AddComponent("Rigidbody");
myNewObjRigbody.isKinematic = true;
myNewObj.gameObject.AddComponent("BoxCollider");
The gameObject gets a rigidbody and a boxcollider, but my raycast does not show up? Do I need to reinitiate something for the raycast to work? does the original file have to be somewhere else as in the resource file, with which I created the prefab. I checked the layers aswell, both are in Default.
thxs Ent
Added complete script: its the: Debug.DrawLine (ray.origin, hit.point); not showing up whats bothering me. Im will add a colliderBox to the prefab to check if its something else. The DistanceToGround is confusing because I havent changed the Var name from the example I had, should be DistanceToCamera.
objectscript (JS):
var counter : int = 0;
var modWidth : int = 900;
var modHeight : int = 900;
var modDepth : int = 900;
var modScale : float = 0.005;
var newObject : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")){
var newPos = new Vector3((modWidth*counter*modScale),0.0,(modHeight*0.5*modScale));
var newScale= new Vector3(modScale,modScale,modScale);
var newRot = new Vector3(270.0,180.0,0.0);
var myNewObj = Instantiate(newObject,Vector3.zero,Quaternion.identity);
var myNewObjRigbody = myNewObj.gameObject.AddComponent("Rigidbody");
myNewObj.localPosition = newPos;
myNewObj.localScale = newScale;
myNewObj.localRotation = Quaternion.Euler(newRot);
myNewObjRigbody.isKinematic = true;
myNewObj.gameObject.AddComponent("BoxCollider");
counter++;
}
}
objectscript (CS):
int counter = 0;
int modWidth = 900;
int modHeight = 900;
int modDepth = 900;
float modScale = 0.005f;
//GameObject newObject;
void Update (){
if (Input.GetButtonDown("Fire1")){
Vector3 newPos= new Vector3((modWidth*counter*modScale),0.0f,(modHeight*0.5f*modScale));
Vector3 newScale= new Vector3(modScale,modScale,modScale);
Vector3 newRot= new Vector3(270.0f,180.0f,0.0f);
GameObject myNewObj = Instantiate(Resources.Load("hocker"),Vector3.zero,Quaternion.identity) as GameObject;
//TYPE?!? myNewObjRigbody= myNewObj.gameObject.AddComponent<Rigidbody>();
myNewObj.transform.localPosition = newPos;
myNewObj.transform.localScale = newScale;
myNewObj.transform.localRotation = Quaternion.Euler(newRot);
//myNewObjRigbody.isKinematic = true;
myNewObj.gameObject.AddComponent<BoxCollider>();
counter++;
}
}
camerascript:
function Update(){
var hit : RaycastHit; var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //Just a check if over GUI or not if (!onMouseOverMenu) { if (Physics.Raycast (ray, hit, 100.0)) { distanceToCamera = hit.distance; Debug.DrawLine (ray.origin, hit.point); }
} }
It's not clear to me from your question, but is there a reason why you can't just setup the prefab to have all the components and settings that you require, so any instance you create will already have them?
I dont always know which collider fits best beforehand, because the meshs will get changed later. So its more like if its object1 use mesh1 and box, if its object2 use mesh2 and Sphere and if it is object3 use mesh3 and mesh collider. I cant make prefabs for all mesh colliders.
Fair enough.
Without testing your code - you're aware that Debug.DrawLine only appears in the Scene View and not in the Game View?
like mentioned, I can see it working with the cubes in the scene, but not the inserted prefabs. Ive tried it in CS now, its the same.
Answer by StephanK · Jun 17, 2010 at 08:30 AM
gameObject.layer = numOfLayer;
Regarding your main question we could use some info. You don't say how you create the ray, where your ground is and if/where you move the new object. If your ground is at (0,0,0) and you create a new object at the same position a raycast will probably not hit this object...
I checked your script and it works for me. Only thing I changed was the camera script:
public class CamerScript : MonoBehaviour {
void Update() {
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//Just a check if over GUI or not
// if (!onMouseOverMenu) { if (Physics.Raycast (ray, out hit)) { float distanceToCamera = hit.distance; Debug.DrawLine (ray.origin, hit.point); }
// } } }
It draws the rays for manually placed items as well as for instantiated prefabs. Maybe you should check out if the colliders are configured correctly on the instantiated prefabs?
I am creating multiple pieces next to each other, so at least one should not be at 0,0,0 :) they are not moved. They are near the cubes I created before, so it should be working.
$$anonymous$$aybe you could post the whole code you have right now. (At least the part that is bothering you) Otherwise all we can do is make wild guesses... ;)
Strange, it works when I create a cube and place this in a prefab. But not when I am using a 3D$$anonymous$$ax Obj file in a prefab. To make a prefab, dont you just 'CREATE PREFAB' and pull the object into the prefab? $$anonymous$$, my mistake :) cant pull resources into the game ^^. Could that be the problem? I just pulled my resource file from the resource folder onto my prefab. It seemed to work but that might be my problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062313im_/https://answers.unity.com/themes/thub/images/avi.jpg)