Search Unity

C# script not working

Discussion in 'Scripting' started by Erin abrams, Oct 9, 2015.

  1. Erin abrams

    Erin abrams

    Joined:
    Mar 4, 2015
    Posts:
    22
    This script is supposed to change the position of the gameobject it is set to in the inspector. I dont know why it wont work though.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotateRoad : MonoBehaviour {
    5.     public GameObject road;
    6.  
    7.  
    8.     void Start () {
    9.  
    10.         road = GameObject;
    11.    
    12.     }
    13.    
    14.  
    15.     void Update () {
    16.  
    17.         if(Input.GetKeyDown("space")
    18.            road.transform.position = (2,2,2);
    19.        
    20.     }
    21. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In Start, you try to set the variable to the class GameObject. Uh... don't do that.

    And check your console for error messages - they'll help us help you (and help you help yourself). That line will definitely cause a compiler error.
     
    Kiwasi likes this.
  3. jinyogi

    jinyogi

    Joined:
    Oct 10, 2015
    Posts:
    1
    road = gameObject
    no GameObject
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is generally no value in caching a components GameObject in start. Nor is a GameObject reference required to access a Transform.