"Error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement" when running game
I have seen a number of posts on this issue but they don't seem to solve my problem. I am creating a game object from a c# script and get the above stated error when I run the code. Please see my script below:
using UnityEngine;
using System.Collections;
public class TestingPositions : MonoBehaviour {
GameObject hero;
Sprite heroSprite;
Vector3 heroPosition;
// Use this for initialization
void Start () {
Instantiate (hero, heroPosition, Quaternion.identity) as GameObject;
Camera camera = GetComponent<Camera>();
heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));
heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>(); renderer.sprite = heroSprite;
}
}
The error is pointing to the line:
Instantiate (hero, heroPosition, Quaternion.identity) as GameObject;
Answer by Brocccoli · Apr 04, 2016 at 08:54 PM
You're using the "as" keyword and you aren't assigning the return value to anything.
Why not
GameObject myObject = Instantiate(x, y, z) as GameObject;
Now I am getting another error:
ArgumentException:The thing you want to instantiate is null. `UnityEngine.Object.CheckNullArgument(System.Object, System.string message)
The thing you want to instantiate is not assigned a value. Just assign it to something.
I suppose I should edit my answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074158im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hey guys I need help for a score broad system. 2 Answers
I have this movement script that works just fine, but how do i integrate jumping and running? 0 Answers
The laser problems 0 Answers
how to make a max number on counter. 1 Answer
Health Script 2 Answers