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 Stormy102 · Jul 06, 2014 at 03:47 PM · javascriptraycastprefab

Placing buildings

I would like to have a script, where if you have a certain amount of logs and stones, you can create a campfire. I want a raycast from the middle of the screen to hit a colldier, and instantate a prefab. I then want this prefab to follow the raycast's position, but all my efforts so far have failed. I will post the code for both of these scripts, the GUI and the handler. Builder_GUI.js #pragma strict

 private var WindowLoc : Rect = Rect(Screen.width/4, Screen.height/4, Screen.width /2, Screen.height /2);
 private var scrollBarChopGrid : Vector2 = Vector2.zero;
 private var ShowBuildingMenu : boolean = false;
 private var Building_Handler : Building_Handler;
 
 var ToggleBuild : KeyCode = KeyCode.P;
 
 function Start()
 {
     Building_Handler = GetComponent("Building_Handler");
 }
 
 function Update()
 {
     if (Input.GetKeyDown(ToggleBuild))
         if (ShowBuildingMenu)
         {
             ShowBuildingMenu = false;
             
         }
         else
             ShowBuildingMenu = true;
 }
 
 function OnGUI () {
     if (ShowBuildingMenu)
         WindowLoc = GUI.Window(0, WindowLoc, Building_Selection, "");
 }
 
 function Building_Selection(windowID : int)
 {
     GUI.Label(Rect(5, 0,100,25), "<b>Item to build:</b>");
     GUI.Label(Rect(110, 0,200,25), "<b>Resources required:</b>");
     scrollBarChopGrid = GUI.BeginScrollView(Rect (0, 25, Screen.width/2,257), scrollBarChopGrid, Rect(0,0,0,500));
         
         GUI.Label(Rect(5, 0,100,25), "Camp Fire");
         GUI.Label(Rect(110,0,200,25), "10xWood, 10xStone");
         if (GUI.Button(Rect(315, 0,75,25), "Build"))
         {
             Building_Handler.Building = true;
             Building_Handler.Once = true;
             ShowBuildingMenu = false;
         }
     GUI.EndScrollView();
 }

Building_Handler.js

 #pragma strict
 
 @HideInInspector
 var Building : boolean = false;
 var BuildCampFire : GameObject;
 
 @HideInInspector
 var Once : boolean = false;
 
 private var hitPointX : int = 0;
 private var hitPointY : int = 0;
 private var hitPointZ : int = 0;
 
 function Update () {
     if (Building)
     {
         var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
         var hit : RaycastHit;
         if(Physics.Raycast (ray, hit, 50))
         {
             var clone : GameObject;
             hitPointX = hit.point.x;
             hitPointY = hit.point.y;
             hitPointZ = hit.point.z;
             if (Once)
             {    
                 clone = Instantiate(BuildCampFire, Vector3(hitPointX,hitPointY + 0.5,hitPointZ), Quaternion.Euler(0,0,0));
                 Once = false;
             }
             clone.position = Vector3(hitPointX,hitPointY,hitPointZ);
             
             if (Input.GetMouseButtonDown(0))
             {
                 Building = false;
             }
         }
     }
 }


Please reply quickly, so I can get this annoying problem fixed.

Thanks!

Stormy102

Comment
Add comment · Show 6
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
avatar image Stormy102 · Jul 06, 2014 at 03:58 PM 0
Share

Anyone able to help?

avatar image KiraSensei · Jul 06, 2014 at 04:06 PM 0
Share

It depends on the problem. So what is yours ?

avatar image Stormy102 · Jul 06, 2014 at 04:07 PM 0
Share

I want the instantiated prefab to follow the raycast hit. That should solve everything.

avatar image Stormy102 · Jul 06, 2014 at 04:14 PM 0
Share

Anyone got any ideas?

avatar image robertbu · Jul 06, 2014 at 06:08 PM 1
Share

@Scribe nailed your problem. $$anonymous$$ove your 'var clone : GameObject' outside of Update(). As a local variable, it loses its value at the end of each Update() call.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Scribe · Jul 06, 2014 at 05:22 PM

Clone is being over written every time so it never saves a reference to the instantiated object, move its initialisation to the top of the handler script, outside of the raycast

Comment
Add comment · Share
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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A way to instantiate an item with 90 Degree increments? 2 Answers

How can I instantiate more prefabs as the score increases? 2 Answers

Prefabs double at each instance? 1 Answer

Grapple Gun? 1 Answer

If Raycast Hits Air 4 Answers


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