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

Scripts changes in Game Window in editor.

Discussion in 'Getting Started' started by unity_75DD2695A8C3C4B4580F, Jun 6, 2023.

  1. unity_75DD2695A8C3C4B4580F

    unity_75DD2695A8C3C4B4580F

    Joined:
    Jun 5, 2023
    Posts:
    5
    How to make scripts changes available in game window in unity. For example in the script are created several prefabs with object SpriteRender and they wont appears in Game window in editor while drag into sceen.
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    1. Make sure you have saved the changes you made to your script.
    2. Make sure there are no compilation errors in the Console window. If there are errors, you need to fix them before the changes can take effect.
    3. Debug your code with Debug.Log() inside the script. Can't find a problem? Simplify the code and check smaller parts of the code.
    If you did everything right It must work. Try to explain what you want to achieve, I had a hard time decoding intentions.
     
  3. unity_75DD2695A8C3C4B4580F

    unity_75DD2695A8C3C4B4580F

    Joined:
    Jun 5, 2023
    Posts:
    5
    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class WallBorder : MonoBehaviour
    5. {
    6.     public GameObject borderPrefab;
    7.     public List<GameObject> borderPrefabObjects;
    8.     public float numberOfBordersF = 0;
    9.     public float length = 4f;
    10.     public Vector3 WallPossition;
    11.  
    12.     void awake()
    13.     {
    14.  
    15.     }
    16.    
    17.     void Start()
    18.     {
    19.  
    20.         BoxCollider2D boxCollider = borderPrefab.GetComponent<BoxCollider2D>();
    21.         Vector2 colliderSize = boxCollider.size;
    22.        
    23.         numberOfBordersF = length / colliderSize.y;
    24.         int numberOfBorders = (int)numberOfBordersF + 1;
    25.  
    26.        
    27.         for (int i = 0; i < numberOfBorders; i++)
    28.         {
    29.        
    30.             float yPos = i * colliderSize.y;
    31.             Vector3 spawnPosition = new Vector3(0f, yPos, 0f);
    32.             spawnPosition += WallPossition;
    33.        
    34.             GameObject wall = Instantiate(borderPrefab, spawnPosition, Quaternion.identity);
    35.             SpriteRenderer sprite = wall.GetComponent<SpriteRenderer>();
    36.             // Debug.Log(sprite.enabled);
    37.            
    38.        
    39.             wall.transform.SetParent(transform);
    40.             borderPrefabObjects.Add(wall);
    41.  
    42.         }
    43.     }
    44. };
    45.  
    Same problem is with backgraound code.
     
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    Have you dragged and dropped the script to exist in the Scene game object so it can use Start? I used your script and everything works. The result was repeated Sprite in line.
     
  5. unity_75DD2695A8C3C4B4580F

    unity_75DD2695A8C3C4B4580F

    Joined:
    Jun 5, 2023
    Posts:
    5
    I'm using a script as a component of an existing object in the game. The problem is that I can't see the rendered elements of this object in the preview within the Unity editor. It requires running the game.
     
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    There is no other way, MonoBehaviour scripts work only when you hit Play.