- Home /
Instantiating prefab with certain coordinates problem (c#)
I am currently having a problem instantiating a prefab for my game's selection script. I want to be able to tap on an object and then check its tag to see if its selectable, then instantiate a selection indicator prefab (game object that has selection ring, and gui elements for object selected) and then set that objects coordinates to the object i tapped, then parent it.
I can raycast to detect which object i hit (using raycasthit, collider, and tags of collider's parent) then i have my selection object instantiate, but it doesn't show up in the correct orientation. It shows up rotated the wrong way, and not aligning with the object at all. The object is also very dark (which i want it to be always lit without light calculations as it is mobile)
How would i make my prefab use the tapped object's location? Here is my code segment:
if (hit.collider.gameObject.CompareTag("Ships")) //if my ray hit a "Ship" tagged object (collider is component and this references the object it is attached to?
{
debug += "\n" + "ship hit" + hit + "\n";//debug string
//ShipVars varScript = (ShipVars)hit.transform.parent.GetComponent("ShipVars");
ShipVars varScript = (ShipVars)hit.collider.GetComponent("ShipVars"); //fetch ShipVars script to see values
if (varScript.isSelected == false) //if ShipVars script says it isnt selected
{
debug += "isSelected: " + "false" + "\n"; //debug string
varScript.isSelected = true; //set ShipVars saying it is selected
//create selected obj here and parent to selected obj ship
//GameObject temp = (GameObject)Resources.Load("/Prefabs/SelectedIndicatorGui",typeof(GameObject)); // this line gave another "cannot convert type to GameObject" error am confused on this as every object that cna exist in game is a GameObject :/
// if(temp == null)
debug += "rayHitObjPos:"+hit.transform.position; // see what values are on the raycasthit collider position
debug+= "RayHitObjRot:"+hit.transform.rotation + "\n"; // see rotation values on raycasthit collider rotation
//GameObject selectedObjGui = (GameObject)Instantiate(guiSel, hit.collider.transform.parent.transform.position, hit.collider.transform.rotation); //instantiate a prefab of my selection object at the colliders current coordinates
GameObject selectedObjGui = (GameObject)Instantiate(guiSel, hit.collider.gameObject.transform.position, hit.collider.gameObject.transform.rotation); //instantiate a prefab of my selection object at the colliders current coordinates
debug+= " step1"; //to see if code was being skipped
//selectedObjGui.transform.parent = hit.collider.transform.parent; //parent my selection object to the ship (does transform.parent work here even if collider is a component?)
selectedObjGui.transform.parent = hit.collider.transform;
selectedObjGui.transform.localPosition = Vector3.zero;
selectedObjGui.transform.localRotation = Quaternion.identity;
debug+="step2" + "\n"; //has executed parenting line
debug+="selObjPos: " + selectedObjGui.transform.position + "localPos: " + selectedObjGui.transform.localPosition;
debug+= " selObjRot: " + selectedObjGui.transform.rotation + "localRot: " + selectedObjGui.transform.localRotation + "\n";
debug += "SelobjName: " + selectedObjGui.transform.name + "\n" + "parName:" + selectedObjGui.transform.parent.transform.name; //make sure that i am parented
debug += "parPos: " + hit.collider.gameObject.transform.position + "ParRot: " + hit.collider.gameObject.transform.rotation + "\n"; //debug showing parent's position showing it is parented and that coordinates are the same (if its not parented this = null)
//adjust scales and locations of selected obj gui
//get collider width / length x = 4 y = 5.2
//scale to biggest in this case it is the y
//scale uniformally to get equal sized selection ring around ship (to accomodate for dif ship sizes)
}
}
I commented the code so you understand what's going on. I tried using the local position/rotation and setting those all to zero (use Vector3.zero and Quaternion.identity) but those gave same results. I used an output string to see the coordinates of the object i am trying to align and the target object, and they are the same.
How can this be? They are obviously not the same, yet my debug string says they are? How can I get my prefab to align with the object i tap on?
Sorry if my code is messy, i tried cleaning it up a bit with comments to help others who read it.
Your answer
Follow this Question
Related Questions
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer
Change Parent of Multiple Instantiated Objects 1 Answer
Prefab, procedural attachment of child, childCount return 0 1 Answer
Is there a way to Instantiate a button prefab as a child of a Canvas? 2 Answers
Instantiate New Gun 1 Answer