Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question size of plane in script of unity

Discussion in 'Scripting' started by deicool1976, Jan 25, 2023.

  1. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Hello

    I am a newbie.

    I would like to change the size of Plane in my C# script. I have done it as follows:

    Code (CSharp):
    1.  RectTransform rt = plane.GetComponent (typeof (RectTransform)) as RectTransform;
    I am getting the following error:
    Code (CSharp):
    1. Assets\planeScript.cs(13,34): error CS1061: 'Plane' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'Plane' could be found (are you missing a using directive or an assembly reference?)
    2.  
    Any ideas?

    Thank You.
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    We'd need to know what "plane" is, and whether you can actually access what you are trying to access.

    We'd also need to know if you are refering to a 4 sided polygon, or a flying machine, when you mention "plane".

    Usually changing of size in engine at runtime is done by adjusting the object's scale, which is under Transform.
     
  3. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Plane is 3D Object (Plane) from Unity IDE

    (I can change the size of Plane in the IDE, however I want to do it in the C# script)
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    If it is a 3d object, it uses Transform, not RectTransform. RectTransform is for canvas UI objects.

    An easier way to write GetComponent is:
    Code (CSharp):
    1. Transform planeTransform = plane.GetComponent<Transform>();
    Although usually, you can access an objects transform by simply doing:
    Code (CSharp):
    1. plane.transform
     
  5. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    I still get the error:

    Code (CSharp):
    1.  'Plane' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'Plane' could be found (are you missing a using directive or an assembly reference?)
    2.  
     
  6. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    I still get the following error:
    Code (CSharp):
    1. Assets\planeScript.cs(13,8): error CS1061: 'Plane' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'Plane' could be found (are you missing a using directive or an assembly reference?)
    2.  
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Are you getting confused by Unity's 'Plane' struct? https://docs.unity3d.com/ScriptReference/Plane.html

    That's all it is, a struct, ergo a simple data representation. Not an actual 'physical' game object in the scene. Neither a game object or a component.

    You probably want to reference an actual game object, or a component on said game object.
     
  8. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,663
    We need to know the type of the "plane" variable in your script. When you declare the variable in the script, you usually specify its type, followed by its name. For instance:

    Code (CSharp):
    1. Transform plane;
    or

    Code (CSharp):
    1. GameObject plane;
    or

    Code (CSharp):
    1. Plane plane;
    etc.

    The error you're getting means that the type you're using does not contain any property with the name "transform". Probably (and this is just me guessing) you've declared your plane as a variable of type Plane. Plane is just the mathematical definition of an infinite plane: a point and a vector, so it doesn't contain any transform, and it certainly doesn't have any size.
     
    Bunny83 and spiney199 like this.
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Well, the error already told us that the type is "Plane" since the error said that the type "Plane" does not contain a method called GetComponent.

    Unity's Plane struct is not a physical "plane" but just an infinite mathematical plane. You can use that plane to raycast against or calculate the distance to that infinite plane. However it's not a gameobject with components. The GetComponent method only exists on GameObjects (or Components which essentially forward the call to GetComponent).
     
    arkano22 likes this.
  10. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Thanks.

    Got it working as below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class planeScript : MonoBehaviour
    6. {
    7.    
    8.         public GameObject plane;
    9.      
    10.         // Update is called once per frame
    11.         void Update ()
    12.         {
    13.             //Transform planeTransform = plane.GetComponent<Transform>();
    14.             plane.transform.position = new Vector3(-2.48f,-1.94f,0.1f);
    15.         plane.transform.localScale = new Vector3(2,2,2);
    16.  
    17.     }
    18.  
    19. }
     
    Last edited: Jan 25, 2023
    arkano22 and spiney199 like this.