Home > Enterprise >  Scaling a gameObject through C# script?
Scaling a gameObject through C# script?

Time:07-15

I am trying to write this script that allows me to toggle a transform change on/off on a sphere. I think the logic is right but I really can't figure out what I'm missing here. (I am also getting (line 7)error CS0108: 'Scale.transform' hides inherited member 'Component.transform'. Use the new keyword if hiding was intended.)

By the way I'm a total beginner, this might be very simple but this is my first game ever!

Here's the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scale : MonoBehaviour
{
    public Transform transform;

    Vector3 scale;
    public float objectSize = 3.0f;

    void ScaleOnOff()
    {
        transform = GetComponent<Transform>();

        if (Input.GetKeyDown(KeyCode.S))
        {
            if (scale.x != objectSize)
            {
                scale = transform.localScale;
                scale.x = objectSize;
                transform.localScale = scale;

                Debug.Log("condition1");
            }

            if (scale.x == objectSize)
            {
                scale = transform.localScale;
                scale.x = 1;
                transform.localScale = scale;

                Debug.Log("condition2");
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
            ScaleOnOff();
    }

}

Also, in the console both of the Debug.Log messages appear, so I'm wondering how can both conditions be true at the same time?

Thanks for your help!

CodePudding user response:

There are a few errors, but that's normal if you are a beginner :).

  • There is already a variable called transform. Unity has some shortcuts for getting components: the most used is transform. If you try to delete the "Transform transform" line, in which you rightly declare a variable, you will see that the code will give you no errors. The variable provided by Unity is the same as GetComponent(), so you can delete this line as well, because transform is by default the same as that.

  • In general, if the variables do not change during the game, as in this case with transform, the values ​​must be taken at the beginning of the game, or at least a few times, not continuously. In your code this is not true because the transform variable as mentioned in the previous point already has a value, but for any other variable you assign a value that does not change, it is best to do it in Awake() or Start().

  • Both messages are shown because the code does the following: if you pressed the button, it checks if the value of scale.x is different from "objectSize". (in the first frame scale.x is equal to 0 because you haven't assigned it yet), If it's true (in the first frame it's true), among other things it does, it makes scale.x equal to objectSize. Continuing the code check if scale.x is equal to "objectSsize" (which in the first frame as explained is true), then also execute the second debug.log(). To solve this problem you can try assigning the value of transform.localScale to "scale" in Start(), and instead use two separate if's, an if and an else. On else you can find a lot of documentation online, so I don't want to explain it here.

    using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class Scale : MonoBehaviour { Vector3 scale; public float objectSize = 3.0f;

     void Start()
     {
         scale = transform.localScale;
     }
    
     void ScaleOnOff()
     {
         if (Input.GetKeyDown(KeyCode.S))
         {
             if (scale.x != objectSize)
             {
                 scale = transform.localScale;
                 scale.x = objectSize;
                 transform.localScale = scale;
    
                 Debug.Log("condition1");
             }
             else
             {
                 scale = transform.localScale;
                 scale.x = 1;
                 transform.localScale = scale;
    
                 Debug.Log("condition2");
             }
         }
     }
    
     // Update is called once per frame
     void Update()
     {
             ScaleOnOff();
     }
    

    }

Excuse me if I was long-winded, but being that you are a beginner, I wanted to explain everything to you well.

Good development!

  • Related