Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision/Color Problems

Discussion in 'Getting Started' started by jwathen, Mar 16, 2017.

  1. jwathen

    jwathen

    Joined:
    Mar 16, 2017
    Posts:
    11
    Hello everyone! I am new to Unity, taking a class in it this semester. We are currently remaking Q*bert in 3D, it's pretty fun but I've been having a problem with the biggest part of the game. The cubes changing colors when the Player hits it.

    I attached the script to the Player(it's a sphere) and the collision works but it changes the color of the sphere not the cube. I've tried putting the script on a prefab called Pyramid, it contains all my cubes, switching the appropriate names in the code. That doesn't work either. I tried it on one of the individual cubes and that didn't work either.

    Here's a picture of my script.

    12.JPG
     
    JoeStrout likes this.
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Hi, Jennifer! Welcome to the forums!

    "Doesn't work" is a difficult issue for us to help debug. You mentioned that when you have this script attached to the player, it changes the player object's color. That doesn't sound right with what I'm seeing in your screenshot, but just to be sure, are you positive you have the GameObjects tagged properly?
     
    jwathen likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What a fun project! Q-Bert was one of my favorite games back in the day.

    I agree with @Schneider21, the script you posted ought to work. When faced with a mystery like this, you want to gather all the clues you can — and in particular, try to think of what assumptions you're making, that you're not really testing. The best/easiest way to do that is to dump more details into your print statement. Instead of just "it worked", do something like:

    Code (csharp):
    1.   print(gameObject.name + " collided with " + other.gameObject.name + ", so changing its material to " + colorOne);
    Other assumptions to check:
    • Is your script really on the object you think it is — and only that object?
    • Do all your objects have the right Tags set on them?
    • Do all of your cubes have a unique material, or are they sharing materials? (Actually shouldn't matter, since you're accessing .material instead of .sharedMaterial, but still.)
    • Other things I'm not thinking of?
    Finally, you asked a good question, but instead of posting a screen shot of code, paste the actual code into your post using the "Insert Code" button in the editing toolbar, right between the movie filmstrip and the floppy disk icon:


    Cheers,
    - Joe
     
    jwathen and Schneider21 like this.
  4. jwathen

    jwathen

    Joined:
    Mar 16, 2017
    Posts:
    11
    I'll try all of those thank you so much! I need to really pick apart more. I will try to go more in depth. School is starting up again so I'm hoping I can get help from a teacher or fellow student. :) Thank you!
     
    SarfaraazAlladin and JoeStrout like this.