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 SpaceFiveFive · Oct 12, 2014 at 02:53 PM · javascriptgameobjectinstantiateraycastbutton

[SOLVED] Only instantiating once

So, I have some code that I am trying to use to create a line over the top of another line (a gameobject), however, that line underneath is made from a prefab. I have the original prefab to the side, but whenever I try to instantiate a line, it only works once, even though I can click on different ones, and also it only pastes it over the prefab to the side. Any ideas?

Code:

 var newSkin : GUISkin;
 var redLine : GameObject;
 var blueLine : GameObject;
 var x : int;
 var y : int;
 var usable : boolean;
 var cube : GameObject;
 var isHit : boolean;
 
 function Start() {
     x = transform.position.x;
     y = transform.position.y;
     usable = true;
 }
 
 function Update() {
     lineButton();
     if (Input.GetMouseButton(0)) {
         var hit : RaycastHit;
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, hit)) {
             if (hit.collider.gameObject.tag==gameObject.collider)
             {
                 isHit = true;
             }
             else
             {
                 isHit = false;
             }
         }
     }
     lineButton();
 }
 
 function lineButton() {
     if(!VarDefiner.isMainMenu)
     {
         if(isHit && usable)
         {
             if(VarDefiner.redTurn)
             {
                 cube = Instantiate(redLine, new Vector3(transform.position.x, transform.position.y, 15), Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + 89.2929));
                 usable = false;
                 isHit = false;
                 VarDefiner.redTurn = false;
             }
             else
             {
                 cube = Instantiate(blueLine, new Vector3(x - 0.5, y + 0.35, 15), Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + 89.2929));
                 usable = false;
                 isHit = false;
                 VarDefiner.redTurn = true;
             }
         }
     }
 }


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 Landern · Oct 12, 2014 at 02:56 PM 0
Share

You had a different expectation? You're instantiating the the prefab into the same cube variable with the same values for placement. When you keep calling lineButton() it is actually doing the same thign over and over. $$anonymous$$ake List. and add the instantiated objection into that. That way you are keeping each instance of the line, not over writing one that exists in memory

avatar image SpaceFiveFive · Oct 12, 2014 at 03:05 PM 0
Share

I thought it would create a new one for every GameObject it was attached to... If it won't, how would I fix this?

avatar image Landern · Oct 12, 2014 at 03:13 PM 0
Share

@SpaceFiveFive, you didn't state that in the question, i could only assume. Yes if you have a script, attached to a bunch of objects and a prefab attached to those scripts in the inspector and you run the script and EACH script dynamically creates and instantiates the prefab, they "SHOULD" be their own copy. If you are dynamically setting the positions and such, whether it's getting it from the parent gameobject or some other scheme, you should be able to see them

avatar image SpaceFiveFive · Oct 12, 2014 at 03:50 PM 0
Share

Sorry about that. The only issue is that they aren't their own copy.

avatar image Landern · Oct 12, 2014 at 04:01 PM 0
Share

@SpaceFiveFive, there isn't enough information, post some screen shots of the scene setup with the scripts attached. Explain the interaction those scripts and the user, this will better help the viewer understand and come up with idea's.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MrSoad · Oct 13, 2014 at 06:58 PM

Have you considered using Unity's "LineRenderer" for your lines. You can give it the start and end point(your dot coordinates) and it will draw a line between the two. You can change how it looks via its Material.

Comment
Add comment · Show 4 · 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
avatar image MrSoad · Oct 13, 2014 at 07:04 PM 0
Share

Should be a lot easier than messing around with pre-made lines and prefabs for what it looks like you are doing.

avatar image SpaceFiveFive · Oct 13, 2014 at 07:09 PM 0
Share

How would I do that, and also keep it a button

avatar image MrSoad · Oct 13, 2014 at 07:14 PM 0
Share

Take a look at this post : http://answers.unity3d.com/questions/470943/collider-for-line-renderer.html For how to do that. i don't see any reason why you cannot use a different collider to the one used here, maybe a 2D box collider rather than a 3D capsule collider.

avatar image SpaceFiveFive · Oct 18, 2014 at 12:57 PM 0
Share

However, I'd like to know why it only creates one, because everything else works.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Create clickable GameObjects (JS) 1 Answer

How to activate a button? 1 Answer

[Logical Error] Select a game object and perform actions using GUI.Button 1 Answer

Instantiated Objects not being set at ground/terrain level?(Solved) 1 Answer

Changing variable in a script of a game object hit with raycast 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