Prefab

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.

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.

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

public class GameControl : MonoBehaviour
{
    public static readonly int totalStick = 54;
    public static readonly int stickPerLayer = 3;
    public time woodstick;
  
    public static int stickNumberVariable=1;
    public static int collisionVariable=0;

    void Start()
    {
        stickNumberVariable=1;
        collisionVariable=0;
        Time.timeScale = 2;
    
        for(int h = 0; h < totalStick/stickPerLayer; h++)
        {
            for(int i = 0; i < stickPerLayer; i++)
            {
                time locStick;
                locStick = Instantiate(woodstick);
                locStick.stickNumber=stickNumberVariable;
                stickNumberVariable++;
                

                if (h % 2 == 0)
                {
                    locStick.transform.position += new Vector3((time.x * i), (time.y * h), 0);
                }
                else
                {
                    locStick.transform.position += new Vector3((time.x * i), (time.y * h), -time.z);
                    locStick.transform.RotateAround(transform.position, new Vector3(0, -1, 0), 90);

                }
                locStick.SetOriginalPosition();
            }
        }
        collisionVariable++;
    }

    public void Restart()
    {
        stickNumberVariable=1;
        collisionVariable=0;
        var sticks = FindObjectsOfType<WoodStick>();
        foreach (WoodStick ws in sticks)
        {
            ws.RevertPosition();
        }

    }


}

Well, here you go:

public time woodstick;

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;

public time woodstick;, here is your error

My prefab name is “time”.

Prefabs are gameobjects, they aren’t given types based on their names

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!

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 :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

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

1 Like

Amazing Intuition. Thanks.

Just something we see thousand of times again and again. :slight_smile:

1 Like