Search Unity

On collision, enable script. Doesn't work.

Discussion in '2D' started by Jip1912, Sep 15, 2015.

  1. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hey,

    I use this script, so when my player hits the grey square, another script from another object is activating. But it doesn't work. The script that has to be activating is called ''RandomSpawn''. I deactivate it, because otherwise the script is activated when I start the game.

    Code (CSharp):
    1.  
    2. public GameObject player;
    3.  
    4.  
    5.     void OnCollisionEnter (Collision col)
    6.  
    7.     {
    8.         if(col.gameObject.name == "GreySquare")
    9.         {
    10.  
    11.             gameObject.GetComponent<RandomSpawn>().enabled = true;
    12.         }
    13.     }
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    if you are using 2d colliders, then you should use: void OnCollisionEnter2D (Collision2D col)

    and be sure, both objects have collider 2D, and one have rigidbody 2d (you can add it to gray square and set it to kinematic)
     
  3. Ceyl

    Ceyl

    Joined:
    Sep 9, 2015
    Posts:
    9
    Also be sure that your object layers can collide each others (see into Edit > project settings > physics 2D).
     
  4. Outwizard

    Outwizard

    Joined:
    Jun 25, 2015
    Posts:
    22
    Also, it's a bit unclear whether this script is attached to a player, or to some other object. If this script is attached to player. is RandomSpawn script also attached to player, because that is what your code looks like?
     
  5. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    This is attached to the player. The randomspawn is attached to another object and if you hit the GreySquare the randomspawn script enables

    I will test the other comments out. ty
     
  6. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I made a new script and used a trigger instead of using collision, but it doesn't seem to work. I attached this script to ''GreySquare'' and I did what you did ''and be sure, both objects have collider 2D, and one have rigidbody 2d (you can add it to gray square and set it to kinematic)''. Also my player really kind of lags right now.


    My script:

    Code (CSharp):
    1.     public GameObject SpawnLine;
    2.  
    3.     void OnTriggerStay2D (Collider2D other)
    4.     {
    5.         Debug.Log ("Het werkt :)");
    6.         gameObject.GetComponent<RandomSpawn>().enabled = true;
    7.     }
    8. }
    Thnx.
     
  7. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
  9. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    Unity has a quick cheat sheet to check which objects produce a physical collision (colliders) or collision event (triggers) :D (scroll to the bottom). There's also some great info about Static vs. Rigidbody/Rigidbody Kinematic Colliders ^_^ Hope that helps!!!
     
  10. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    - No
    - Yes the greysquare has a rigidbody2d with kinematic.
    - Yes

    How would a script look like? It looks really complicated. And why does my method not work for me.
    Physics2D.OverlapCircle
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    put a bool on your spawn script & set it to false.
    When your player hits the grey box access the spawn script & set the bool to true.
    In the spawn script Update use an if statement so the spawn only works if the bool is true.
    In that if statement, after the spawning has finished, set the bool back to false so it stops spawning.
     
  12. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I will try this out, thanks.
     
  13. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hey,

    I made my script like this, the script does turns off, that works. But it doesn't turn on.

    Code (CSharp):
    1. void Start ()
    2.     {
    3.         gameObject.GetComponent<RandomSpawn> ().enabled = false;
    4.     }
    5.  
    6.    
    7.     void Update()
    8.  
    9.             {
    10.                 if (gameObject.GetComponent<RandomSpawn> ().enabled = true);
    11.  
    12.             {
    13.                 int randomNumber = Random.Range (0, positions.Length);
    14.                 transform.localPosition = positions [randomNumber];
    15.  
    16.                 if (transform.position.x > -3 && transform.position.x < -2) {
    17.                     transform.rotation = Quaternion.Euler (0, 0, 90);
    18.  
    19.  
    20.                 }
    21.  
    22.             }
    23.  
    24.         }
    25.  
    26. }
    I also get this error:
    https://gyazo.com/da53ee00dcc930fd4c534ff6ca27731a

    This is my script on my ''GreySquare'' (that is where the error came from)

    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    4.         {
    5.             gameObject.GetComponent<RandomSpawn> ().enabled = true;
    6.             Debug.Log ("It should collide now");
    7.         }
    8.     }
    9. }
    I tagged my player with ''Player''.

    The ''GreySquare'' has a rigidbody 2d with kinematic and a box collider 2d, with ''is trigger'' turned on.
    The ''Player'' has a box collider 2d only.

    I hope to hear from you soon.

    Thanks
     
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you tried putting a breakpoint on the line that is setting the bool to true so you can check that it is finding it to update it? If it isn't then there is an issue with the way you are trying to pass that across.
    Also, why are you doing get component to set the bool on the spawner? If the spawn script is on the spawn point then the spawn script only néeds to have the bool without all the get component stuff.
     
  15. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Sorry but I don't understand what you mean with this?

    Sorry, haven't said that I think, but the spawn script is on the spawn object (the line), it isnt on the ''GreySquare''
     
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If the spawn script is on the spawn object then it doesn't need the get component to set the bool to false since the script is on that component.
    A breakpoint is a debug tool. You set it on the line you think is causing the issue then run the game while using the debugger. It can be slow as it needs to load everything so give it a chance before you panic. The game will then run & when it goes to action the line/s with break points it will stop & flick to the code & by hovering the mouse over variables etc you can see the values before it runs, you then step into the code & see what the value is after the line runs. If your breakpoint is never called then you know your code has an issue as it isn't actually processing your line.

    http://docs.unity3d.com/432/Documentation/Manual/Debugger.html
     
  17. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    If I don't need to use ''get component'', how should I do it than? Shouldn't it work with this?
     
  18. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I think once you turn a game object off it isn't there any more to be found to turn back on. Research using break points & put one in at the collision & see what happens when it tries to find the component to turn it back on.
     
  19. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Can someone pls help me with this.
     
  20. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I actually think the issue is caused by enabling/disabling the object. Leave it always on & just use a bool on your spawner that is set to false, turns to true when the player hits the grey box & turns back to false after it has spawned however many items you want.
     
  21. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Thnx, I will try it out, and let you know.
     
  22. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hi,

    I tried this out, I'm new to coding so I maybe made some mistakes.
    There are no errors, I get a debug.log but there is no line spawning.

    Randomspawn script:

    Code (CSharp):
    1.     public Vector3[] positions;
    2.     public int spawn = 1;
    3.  
    4.     void Start ()
    5.     {
    6.         gameObject.GetComponent<RandomSpawn> ().enabled = false;
    7.     }
    8.  
    9.      
    10.     void Update()
    11.  
    12.             {
    13.                 if (spawn == 2);
    14.  
    15.             {
    16.                 int randomNumber = Random.Range (0, positions.Length);
    17.                 transform.localPosition = positions [randomNumber];
    18.  
    19.                 if (transform.position.x > -3 && transform.position.x < -2) {
    20.                     transform.rotation = Quaternion.Euler (0, 0, 90);
    21.  
    22.  
    23.                 }
    24.  
    25.             }
    26.  
    27.         }
    28.  
    29. }
    30.  
    GreySquareTrigger script:

    Code (CSharp):
    1.     public int spawn;
    2.  
    3.     void OnTriggerEnter2D (Collider2D other)
    4.     {
    5.         if (other.gameObject.tag == "Player")
    6.         {
    7.             spawn = 2;
    8.             Debug.Log ("It should collide now");
    9.         }
    10.     }
    11. }
    So I made those ''int''.
    Before unity I used gamemaker, so my script might look like that.

    Thankyou for helping
     
  23. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    On the spawn object just have the script with a bool (e.g canSpawn) on it that is set to false. Don't make the game object / enabled false or the component is turned off & nothing can interact with it.

    On the trigger script you need to create a reference to the script on the spawner. I don't have access to my scripts so this is likely wrong but there are plenty of answers around including unity stuff on how to access variables on other scripts. Try putting a public GameObject on the trigger script called spawner or something & in the inspector drag the spawn object into the slot. In the trigger script, if the player collides then spawner.canSpawn=true;

    On the spawn script your if statement will be
    if(canSpawn){
    Do all your spawn stuff
    canSpawn=false; //turn spawn off once completed
    }
     
  24. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hi,

    Changed the script with:

    Code (CSharp):
    1.     public Vector3[] positions;  
    2.     public bool spawn;
    3.    
    4.     void Start()
    5.     {
    6.         spawn = false;
    7.     }
    8.    
    9.     void Update()
    10.        
    11.     {
    12.         if (spawn == true)
    13.            
    14.         {
    15.             int randomNumber = Random.Range (0, positions.Length);
    16.             transform.localPosition = positions [randomNumber];
    17.            
    18.             if (transform.position.x > -3 && transform.position.x < -2) {
    19.                 transform.rotation = Quaternion.Euler (0, 0, 90);
    20.                
    21.                 spawn = false;
    22.             }
    23.            
    24.         }
    25.        
    26.     }
    27.    
    28. }
    29.  
    and

    Code (CSharp):
    1. public class GreySquareTrigger : MonoBehaviour {
    2.     public bool spawn;
    3.  
    4.     void OnTriggerEnter2D (Collider2D other)
    5.     {
    6.         if (other.gameObject.tag == "Player")
    7.         {
    8.             spawn = true;
    9.             Debug.Log ("It should collide now");
    10.         }
    11.     }
    12. }
    It looks like the GreySquareTrigger script isnt matched with the RandomSpawn script. Because it doesnt turn on true if I collide with the grey square.
    I don't really understand youre tip with the gameobject.
     
  25. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Sorry for the delay, it's school holidays here so I've been busy with the kids. I did this as a quick test in 3D so you will need to fix it up for 2d. I also just had 1 empty gameobject as the spawnpoint & instantiated an object at it's co-ordinates. The public gameobject cube is the prefab I spawned, the one called spawnPoint is the empty gameobject I used as the co-ordinates to spawn the new item at & it was holding the TestSpawnScript I created.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GreySquareTrigger : MonoBehaviour {
    5.  
    6.     public GameObject spawnPoint;
    7.     TestSpawnScript spawnScript;
    8.  
    9.     void Start() {
    10.  
    11.         spawnScript = spawnPoint.GetComponent<TestSpawnScript> ();
    12.  
    13.     }
    14.  
    15.  
    16.     void OnTriggerEnter (Collider other)
    17.     {
    18.         if (other.gameObject.tag == "Player")
    19.         {
    20.             spawnScript.spawn = true;
    21.             Debug.Log ("It should collide now");
    22.         }
    23.     }
    24. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestSpawnScript : MonoBehaviour
    5. {
    6.  
    7.     // public Vector3[] positions;
    8.     public bool spawn;
    9.     public GameObject cube;
    10.  
    11.     void Start ()
    12.     {
    13.         spawn = false;
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         if (spawn == true) {
    19.             Instantiate (cube, transform.position, transform.rotation);
    20.             spawn = false;
    21.         }
    22.          
    23.     }
    24.      
    25. }
    26.    
    Hope this helps, for some reason it sometimes spawns 1 & other times 2 cubes but I don't have time to work out why. To get around it I destroyed the GreySquareTrigger after your debug.log line by inserting

    Destroy(gameObject);

    If you only need the trigger to be used once that should work for you.
    Hopefully it gives you a starting point.
     
    Last edited: Oct 4, 2015
  26. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Thank you, I will try this out.
     
  27. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I dind't know what you ment with some things (the objects and the testscript??)
    I made my codes like this, but it doesn't work (can you please show me how to do it with my scripts?):

    Code (CSharp):
    1. public class RandomSpawn : MonoBehaviour {
    2.    
    3.     public Vector3[] positions;  
    4.     public bool spawn;
    5.    
    6.     void Start()
    7.     {
    8.         spawn = false;
    9.     }
    10.    
    11.     void Update()
    12.        
    13.     {
    14.  
    15.         if (spawn == true)  
    16.         {
    17.             int randomNumber = Random.Range (0, positions.Length);
    18.             transform.localPosition = positions [randomNumber];
    19.            
    20.             if (transform.position.x > -3 && transform.position.x < -2) {
    21.                 transform.rotation = Quaternion.Euler (0, 0, 90);
    22.                
    23.                 spawn = false;
    24.             }
    25.            
    26.         }
    27.        
    28.     }
    29.    
    30. }
    31.  
    Code (CSharp):
    1. public class GreySquareTrigger : MonoBehaviour {
    2.  
    3.     public GameObject spawnPoint;
    4.     RandomSpawn spawnScript;
    5.    
    6.     void Start() {
    7.        
    8.         spawnScript = spawnPoint.GetComponent<RandomSpawn> ();
    9.        
    10.     }
    11.    
    12.    
    13.     void OnTriggerEnter (Collider other)
    14.     {
    15.         if (other.gameObject.tag == "Player")
    16.         {
    17.             spawnScript.spawn = true;
    18.             Debug.Log ("It should collide now");
    19.         }
    20.     }
    21. }
    Thanks.
     
  28. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Fix the ontrigger so it is for 2d (I made it using 3D objects).
    Check your player is tagged correctly.
    Where are you instantiating the new objects? It looks like you are just changing the position of the random spawn point.
    Did you drag the object into the inspector on the grey cube?
     
  29. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I don't know what you mean with those objects, like spawnpoint. What do I place there and why?
     
  30. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I used spawn point as the coordinates that I wanted the enemy to spawn at as I didn't want to make an array of positions as I didn't have the time. You called your script random spawn so I assumed you were wanting to spawn new items when you collided with the grey cube. If that's the case you haven't put an instantiation line anywhere in your code, I can't tell what you are doing with the random spawn code except changing the location of whatever it is attached to.
     
  31. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    It aren't enemy's but just ''lines'' to slide over. I used this tutorial:

    It doesn't duplicate an object, it just random places the object at one of the 3 filled in coordinates (in my case 3).
     
  32. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    So you want to move the cube that was hit to a different spot? If so you could combine the 2 scripts, I can't see a reason why it wouldn't work to have the trigger check in the cube & if the trigger is the player then move the cube to the new position.
     
  33. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    xD
    That is where this thread is about. The script I use doesn't work. I used a trigger.

    So I show my scripts again:

    The GreySquareTrigger:

    Code (CSharp):
    1.     public GameObject spawnPoint;
    2.     RandomSpawn spawnScript;
    3.    
    4.     void Start() {
    5.        
    6.         spawnScript = spawnPoint.GetComponent<RandomSpawn> ();
    7.        
    8.     }
    9.    
    10.    
    11.     void OnTriggerEnter2D (Collider2D other)
    12.     {
    13.         if (other.gameObject.tag == "Player")
    14.         {
    15.             spawnScript.spawn = true;
    16.             Debug.Log ("It should collide now");
    17.         }
    18.     }
    19. }
    The RandomSpawn script:

    Code (CSharp):
    1.     public Vector3[] positions;  
    2.     public bool spawn;
    3.    
    4.     void Start()
    5.     {
    6.         spawn = false;
    7.     }
    8.    
    9.     void Update()
    10.        
    11.     {
    12.  
    13.         if (spawn == true)  
    14.         {
    15.             int randomNumber = Random.Range (0, positions.Length);
    16.             transform.localPosition = positions [randomNumber];
    17.            
    18.             if (transform.position.x > -3 && transform.position.x < -2) {
    19.                 transform.rotation = Quaternion.Euler (0, 0, 90);
    20.                
    21.                 spawn = false;
    22.             }
    23.            
    24.         }
    25.        
    26.     }
    27.    
    28. }
    29.  
    SpawnLine components.

    2015-10-06_09-59-57.png

    GreySquareTrigger components:

    2015-10-06_10-00-56.png

    Player components:

    2015-10-06_10-02-05.png

    Soooo..... What's the problem ^^

    Thanks for helping :D
     
  34. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    Hey tedthebug, can you still help me with this?
     
  35. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
  36. acemary

    acemary

    Joined:
    Oct 26, 2015
    Posts:
    2
    Really? I don't recall that happening at all. Are you saying when you get teleported and you move slightly but are still inside the trigger it'll say you've exited? If you don't move at all after teleportation does it do anything?

    [EDIT]

    You know what I recall doing. In my video I recall linking each teleporter via a script variable so that I can manipulate the other teleporters script variables because when you teleport over to the other one you have to set it's variable to prevent what you are talking about, and getting telported back. Did you watch the video because I think I go over that in there. I haven't watched it in some time but I recall having to do something like that. Linking each teleporter to each other, but I don't see that in your script. .....http://www.traininginsholinganallur.in/web-designing-training-in-chennai.html | http://www.trainingintambaram.in/php-training-in-chennai.html
     
  37. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
  38. Jip1912

    Jip1912

    Joined:
    Mar 13, 2015
    Posts:
    314
    I dont understand what you guys say, because 1. The player doesn't TP the line does. So it actually makes a path for you. One of the 3 paths are possible. The only problem is that I can't make it so that if I stand on a trigger, the random line spawns. Because now it spawns automaticly when the game starts.
     
  39. plusplusgames

    plusplusgames

    Joined:
    Jul 26, 2021
    Posts:
    69
    did you find a fix yet
     
  40. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Let's not necro a post from 2015 replying to a user who last posted over 3 years ago. It just adds noise.

    Thanks.
     
  41. plusplusgames

    plusplusgames

    Joined:
    Jul 26, 2021
    Posts:
    69
    i was just joking
     
  42. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    By asking for a fix? Okay.
     
  43. plusplusgames

    plusplusgames

    Joined:
    Jul 26, 2021
    Posts:
    69
    I was joking because it was an old post I thought pottetionally the user may see it and think "ha I remember this post"
     
  44. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    No matter what your reasons are, please read the Unity Community Code of Conduct, specifically point 1i which states "Pointless necroposting (posting in a forum thread that is too old to matter any more, or has served its purpose)".

    The forums are here to help though; if you have an issue then please create a new thread and provide some information, A necro becomes less of a problem if you provide something potentially useful or relevant to others also but a new thread is free and likely to get more help that an old thread like this.