Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 1337GameDev · Dec 21, 2011 at 05:02 AM · instantiateprefabparentraycasthitlocation

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges