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 /
This question was closed May 17, 2013 at 06:25 PM by Eugenius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Eugenius · May 13, 2013 at 09:32 PM · collisioninstantiate

Stop instantiate on collision from other script

Hey guys,

This question comes in completion of my previous one "How to limit instantion of prefabs/gameobjects".

So I have a script called "CreateWall" that I use to instantiate objects in certain directions on mouse click&drag.

The instantiation code looks something like this:

 function SpawnFence()
 {
 
 if (sss == 1 && !someCol.someBool)
         {
             position_var = clone2.position + Vector3(0,20,0);    
             clone3 = Instantiate(wallObject, position_var, Quaternion.identity);
             clone3.parent = parentofwall;
             clone3.transform.position.z = 0;
             last_clone = clone3;
             
             if(!someCol.someBool){
             sss = 2;
             }
         }

and it goes on and one until the last clone.

The collision script is currently set on the object that is being instantiated and it has the following code:

 function Awake()
 {
     wallCreate = GameObject.Find("Background - GameManager").GetComponent.<CreateWall>();
     someBool = false;
 }
 
 function OnTriggerEnter(hit:Collider)
 {
     if(hit.collider.tag == "SomWall")
     {
         wallCreate.sss = 0;
         Debug.Log ("collided");    
         someBool = true;    
     }
 }

Here's my problem. The sss var is being set to 0 in my CreateWall, my someBool is becoming true right when colliding but i think objects are instantiating too fast for the script to have time to run through this. Should I slow it down using WaitForSeconds or should I rewrite my whole script?

SEE LAST COMMENT FOR MORE INFO ON THE FIX!

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by DannyB · May 15, 2013 at 08:38 PM

A few things are noticeable:

First, it would always be easier for you (and for us to understand) if you give the variables more meaningful names.

Now, as for the essence of this question - I would assume your problem is that the sss variable is being controlled by both an outside source (your collision detection) and the condition itself, which I can assume is in a loop.

So, as a first test, separate the two. Let the collider control a different variable that is untouched by anyone other then the collider itself ( like wallCreate.isCreationEnabled = false ).

If I am reading your code correctly, I have a feeling this approach will point you in the right direction.

Comment
Add comment · Show 3 · 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 Eugenius · May 15, 2013 at 08:42 PM 0
Share

Thank you for the tip, I actually already did both those things with my code. And the collision is triggered (I see the debug message) and also a boolean becomes true.

I'm not sure where my problem stands. I've tried to turn the boolean to static (so I can access it from the other script) but I'm not sure whether it's triggered or not, although the debug message is still sent).

avatar image DannyB · May 15, 2013 at 08:49 PM 1
Share

I see. There is another alternative to static or GameObject.Find() that may help you - the "pseudo-singleton" of Unity.

In your WallCreate object, make a variable public static WallCreate instance;, then in its Awake() do instance = this;, and finally, you can now get a reference to it statically, by using WallCreate.instance ins$$anonymous$$d of your GameObject.Find.

avatar image Eugenius · May 17, 2013 at 06:24 PM 0
Share

Fixed. I'm marking your answer as correct because it's close enough!

If someone ever needs more info on this - I solved it by creating clones only if a variable in the other script was not met (eg: the collision did not occur).

Follow this Question

Answers Answers and Comments

15 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

Related Questions

How To: Spawn object on raycast collision. 2 Answers

Collision with tag 5 Answers

Issues with instantiated prefabs 1 Answer

Set parent of instantiated object. 0 Answers

Checking Collision on instantiated object 0 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