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

Resolved I don't know how to change my balls color.

Discussion in 'Editor & General Support' started by SpokenPlayer266, Apr 13, 2021.

  1. SpokenPlayer266

    SpokenPlayer266

    Joined:
    Apr 13, 2021
    Posts:
    2
    Hi, I'm a little new to unity so this might not be such a big problem.

    I made this soccer game with four walls surrounding the field and goals on both sides.
    The middle of the field is (X = 0, Y = 0, Z = 0)
    When the ball's Z coordinate becomes negative Z. Or (0, 0, -1) I want the ball to change to the color Red
    When the ball's Z coordinate becomes positive Z. Or (0, 0, 1) I want the ball to change to the color Blue.
    I'm having problems trying to get the script to find my balls Z coordinate. I just typed transform.position.Z, but I'm pretty sure that's now how you do it.

    Can anyone help?

    PS, I have images at the bottom. I hope they help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ballthings : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public float ballPushSpeed;
    9.     public Color myColor;
    10.     public float rFloat;
    11.     public float gFloat;
    12.     public float bFloat;
    13.     public float aFloat;
    14.     //colors go from 0 to 1
    15.     //defult is 0
    16.     public Renderer myRenderer;
    17.    
    18.  
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         rb = GetComponent<Rigidbody>();
    24.         myRenderer = gameObject.GetComponent<Renderer>();
    25.         ballChangeColor();
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.  
    32.     }
    33.  
    34.     private void OnCollisionEnter(Collision collision)
    35.     {
    36.         //Debug.Log("Entered");
    37.         if (collision.gameObject.tag == ("Player"))
    38.         {
    39.             transform.Translate(new Vector3(ballPushSpeed * Time.deltaTime, 0, ballPushSpeed * Time.deltaTime));
    40.         }
    41.     }
    42.    
    43.    
    44.    
    45.     void ballChangeColor()
    46.     {
    47.         if (transform.position.Z < 0)
    48.         {
    49.             rFloat = 1;
    50.             bFloat = 0;
    51.             gFloat = 0;
    52.             aFloat = 1;
    53.         }
    54.  
    55.         else if (transform.position.Z > 0)
    56.         {
    57.             rFloat = 0;
    58.             bFloat = 1;
    59.             gFloat = 0;
    60.             aFloat = 1;
    61.         }
    62.         else if (transform.position.Z == 0)
    63.         {
    64.             rFloat = 0.5f;
    65.             bFloat = 0.5f;
    66.             gFloat = 0;
    67.             aFloat = 0;
    68.         }
    69.         myColor = new Color(rFloat, bFloat, gFloat, aFloat);
    70.         myRenderer.material.color = myColor;
    71.     }
    72. }
    Soccer game - Bottom half of the inspector.PNG Soccer game - Code for the ball, first 32 lines..PNG Soccer game - Code for the ball, line 33 to 64.PNG Soccer game - Code for the ball, line 65 - 71.PNG Soccer game - error message.PNG
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    transform.position.Z works. What problem are you having?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    transform.position.z
    < should be lowercase
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Oops. Good catch.
     
  5. SpokenPlayer266

    SpokenPlayer266

    Joined:
    Apr 13, 2021
    Posts:
    2
    Thanks, I'll let u know if there's still a problem. Sorry for the hold up, didn't notice things get answered so quickly.

    Edit, thanks so much it works.
     
    Last edited: Apr 15, 2021