Error using Texture2D.getPixle() - texture2d

The error is
NullReferenceException: Object reference not set to an instance of an object mapS.Start () (at Assets/mapS.cs:25)
The part with the error in the code
Texture2D img = Resources.Load(#"\images\1") as Texture2D
Color try1 = img.GetPixel(0,0);
print(try1.ToString());

The texture's read/write option was disabled

Related

Can't instantiate "scene object" [Unity Mirror Multiplayer]

I am trying to instantiate an object that already exists in the scene so that I can enlarge it and "zoom" in on it, but when I play the game and hover over an object, instead of showing the zoomed card, this error shows up:
Card1(Clone)(Clone) has already spawned. Don't call Instantiate for NetworkIdentities that were in the scene since the beginning (aka scene objects). Otherwise the client won't know which object to use for a SpawnSceneObject message.
NOTE: I am following a tutorial for a 2D Unity Card game with mirror (2019.2.15f1), and I am using a different version of Unity (2020.1.0f1).
First, I am instantiating objects into my game with this script, which never throws any errors.
[Command]
public void CmdDealCards()
{
for (int i = 0; i < 4; i++)
{
GameObject card = Instantiate(playerDeck[Random.Range(0, playerDeck.Count)], new Vector2(0, 0), Quaternion.identity);
NetworkServer.Spawn(card, connectionToClient);
RpcShowCard(card, "Dealt");
}
}
The problem comes when I try to instantiate these objects again later, here:
public void OnHoverEnter()
{
zoomCard = Instantiate(gameObject, new Vector2(Input.mousePosition.x, 540), Quaternion.identity);
zoomCard.transform.SetParent(Canvas.transform, true);
zoomCard.layer = LayerMask.NameToLayer("Zoom");
RectTransform rect = zoomCard.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(240, 354);
}
I know this is a really late answer, but I came across it while running into the same issue, and maybe in the future, someone will do the same as me, so:
What you've done is attached the wrong prefab to your CardZoom script in the Inspector.
Image of Inspector with Zoom Card attached
What I did, and presumably you also, was to attach Card1 here, which is where it was failing to duplicate the NetworkIdentity

Cant instantiate multi-sprite

I can't seem to load a sprite runtime.
I have a main grid object, on the grid I have a TerrainDrawer component.
The code for the script :
void Start()
{
Sprite[] myFruit = Resources.LoadAll<Sprite>("Sprites/multisprite");
foreach(var sprite in myFruit)
{
print("sprite : " + sprite.name);
}
/*var spritePath = "Sprites/225835_hyptosis_tile-art-batch-1";
//GameObject go = new GameObject();*/
SpriteRenderer renderer = gameObject.AddComponent<SpriteRenderer>();
renderer.sprite = myFruit[0];
//renderer.sprite = Resources.Load(spritePath , typeof(Sprite)) as Sprite;
}
The sprite that I'm wanting to use:
When I start up my game, I get this error :
IndexOutOfRangeException: Index was outside the bounds of the array.
TerrainDrawer.Start () (at Assets/Scripts/TerrainDrawer.cs:21)
I have a strong feeling that my problem is with the path to the sprite, but now matter what I try, I can't get it loaded up.
Any advice?
To load from Resources in code there must exist a folder named Resources with that sprite in it. You are basically loading in an empty array and that is why you get an out of range error.
Take a look at this page from the docs.
Hope this helps!

Unity: Instantiating prefab on GameObject - Error Object is null

In my Unity game I have a vehicle prefab working with Edy's Vehicle Physics asset. I'm trying to instantiate a chosen vehicle prefab on the position and rotation of a GameObject.
When running my code in my scene to spawn a vehicle I get this error
ArgumentException: The Object you want to instantiate is null
ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:151)
UnityEngine.Object.Instantiate[VehicleController] (EVP.VehicleController original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206)
VehicleSpawner.spawnVehiclesInFirstGarage () (at Assets/Scripts/VehicleSpawner.cs:44)
HandleGarage.Start () (at Assets/Scripts/HandleGarage.cs:59)
Vehicle Prefab is empty because the vehicle will be chosen by the user in game. (Even if I select a vehicle prefab I still get same error)
My code for instantiating the vehicle prefab on the game object
public EVP.VehicleTelemetry telemetryComponent;
public EVP.VehicleController vehiclePrefab;
public GameObject spawnObject;
public void spawnVehiclesInFirstGarage ()
{
// Load saved JSON
string jsonData = SecurePlayerPrefs.GetString ("vehicleNames");
// Convert to Class
Database loadedData = JsonUtility.FromJson<Database> (jsonData);
// Loop through Owned Vehicles Garage
for (int i = 0; i < loadedData.vehicleNames.Count; i++) {
// Set up instantiate
var vehicle = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i]));
vehiclePrefab = vehicle as EVP.VehicleController;
// Spawn vehicle on game object
EVP.VehicleController newVehicle = Instantiate(vehiclePrefab, spawnObject.transform.position, spawnObject.transform.rotation) as EVP.VehicleController;
telemetryComponent.target = newVehicle;
}
}
Possible Solutions
The vehiclePrefab = vehicle as EVP.VehicleController; could be causing problems, as logging var vehicle = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i]));, already gives Sport Coupe (UnityEngine.GameObject), which is the correct type.
Instead of using as EVP.VehicleController, try:
// Set up instantiate
var vehiclePrefab = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i])) as GameObject;
// Spawn vehicle on game object
GameObject newVehicle = Instantiate(vehiclePrefab, spawnObject.transform.position, spawnObject.transform.rotation);
EVP.VehicleController controller = newVehicle.GetComponent<EVP.VehicleController>();
telemetryComponent.target = newVehicle;
This is from Unity's documentation for loading prefabs via scripting.
From the error message, it seems that spawnVehiclesInFirstGarage() is being called in void Start(). If so, try putting it in the void Update(), adding a if( prefab != null && !isInstantiated) check.
Additional Troubleshooting (if the solutions don't work)
Try assigning a prefab via the inspector and see if you get the same problem.
On your existing code, try Debug.Log(vehiclePrefab).

How do I get my terrain to self-delete?

I set up this code in the deletion script, first off:
var terrain = GetComponent(Rigidbody2D);
function Update ()
{
if (terrain.position.x <= Camera.main.transform.position.x - 5)
{
Destroy(this.GameObject);
}
}
Here is a picture of the screen:
And my prefab stuff:
What I want is for, when the camera is in front of the prefab, I want it to delete itself. However, this is the error I get:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Component.GetComponent (System.Type type)
Deletion..ctor () (at Assets/Scripts/Deletion.js:3)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
Repeating Terrain:SpawnTerrain(Object) (at Assets/Scripts/Repeating Terrain.js:60)
Repeating Terrain:Update() (at Assets/Scripts/Repeating Terrain.js:52)
So, it turns out, I just needed to replace this.GameObject with this.gameObject. I capitalized it, that was all.
Try it with a timer and use console/debug to make sure you have the right object
Destroy (this.GameObject, 5);
or
GameObject.Destroy(gameObject)

Unity3D: Instantiate creates NullReferenceException

Transform child = GameObject.FindWithTag("state").transform;
child.name = first_guy+""; child.tag = first_guy+"";
GameObject child_dup = Instantiate( child, new Vector3(0,0,0),Quaternion.identity) as GameObject;
Debug.Log (child_dup.name);
The above code gives me error, "NullReferenceException: Object reference not set to an instance of an object".
Is this because I am trying to instantiate a local variable? How to duplicate a gameobject in run time?
Your problem is that you are trying to instantiate a Transform which is a component and cannot be instantiated, as it must be attached to a GameObject.
Instantiate( child.gameObject, ...)