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

Prefab

Discussion in 'Scripting' started by deicool1976, Aug 18, 2022.

  1. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Hello

    There is a Prefab already existing in Unity Project. When I rename the prefab and try to use it in c# script, I get an error.

    Assets\Scripts\GameControl.cs(9,12): error CS0246: The type or namespace name 'time' could not be found (are you missing a using directive or an assembly reference?)

    What do I need to do?

    Please advise.

    Thanks.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    Most likely prefab has nothing to do with it, but there's a typo in the script. It is hard to tell what ort of typo, because you've not posted any code.

    It is possible, however, t hat it is something like "time.deltaTime" instead of "Time.deltaTime".

    C# is case sensitive.
     
  3. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GameControl : MonoBehaviour
    7. {
    8.     public static readonly int totalStick = 54;
    9.     public static readonly int stickPerLayer = 3;
    10.     public time woodstick;
    11.  
    12.     public static int stickNumberVariable=1;
    13.     public static int collisionVariable=0;
    14.  
    15.     void Start()
    16.     {
    17.         stickNumberVariable=1;
    18.         collisionVariable=0;
    19.         Time.timeScale = 2;
    20.    
    21.         for(int h = 0; h < totalStick/stickPerLayer; h++)
    22.         {
    23.             for(int i = 0; i < stickPerLayer; i++)
    24.             {
    25.                 time locStick;
    26.                 locStick = Instantiate(woodstick);
    27.                 locStick.stickNumber=stickNumberVariable;
    28.                 stickNumberVariable++;
    29.                
    30.  
    31.                 if (h % 2 == 0)
    32.                 {
    33.                     locStick.transform.position += new Vector3((time.x * i), (time.y * h), 0);
    34.                 }
    35.                 else
    36.                 {
    37.                     locStick.transform.position += new Vector3((time.x * i), (time.y * h), -time.z);
    38.                     locStick.transform.RotateAround(transform.position, new Vector3(0, -1, 0), 90);
    39.  
    40.                 }
    41.                 locStick.SetOriginalPosition();
    42.             }
    43.         }
    44.         collisionVariable++;
    45.     }
    46.  
    47.     public void Restart()
    48.     {
    49.         stickNumberVariable=1;
    50.         collisionVariable=0;
    51.         var sticks = FindObjectsOfType<WoodStick>();
    52.         foreach (WoodStick ws in sticks)
    53.         {
    54.             ws.RevertPosition();
    55.         }
    56.  
    57.     }
    58.  
    59.  
    60. }
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    Well, here you go:
    Code (csharp):
    1.  
    2. public time woodstick;
    3.  
    There's no type called "time".

    What is it even supposed to do? If you're trying to instantiate an object from prefab reference, it should be
    public GameObject woodstick;
    , or, seeing that you have a WoodStick behavior somewhere,
    public WoodStick woodstick;
     
  5. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    public time woodstick;
    , here is your error
     
  6. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    My prefab name is "time".
     
  7. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,751
    Prefabs are gameobjects, they aren't given types based on their names
     
  8. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Funny Thing. If I rename a Prefab object (say 'WoodStick' to 'WoodStick1'). And then again rename it (to 'WoodStick' from 'WoodStick1'), the game stops working!
     
  9. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    When I try to instantiate an prefab, I get the following error:

    ArgumentException: The Object you want to instantiate is null.
    UnityEngine.Object.Instantiate[T] (T original) (at <d3b66f0ad4e34a55b6ef91ab84878193>:0)
    GameControl.Start () (at Assets/Scripts/GameControl.cs:28)

    Any idea what this could be?

    P.S: I am using Unity 2020.3.26f1 Personal
     
    Last edited: Aug 19, 2022
  10. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    As the log says, the prefab/object that you are trying to instantiate is null. Lucky guess - double check if you assigned it in the inspector
     
    Bunny83 likes this.
  11. deicool1976

    deicool1976

    Joined:
    Dec 6, 2021
    Posts:
    76
    Amazing Intuition. Thanks.
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    Just something we see thousand of times again and again. :)
     
    Bunny83 likes this.