c# - Rotate dynamically created object only if it exists? -


in code below, error (as expected) on line 7:

the name "cube" not exist in current context.

1. if (action == "place") { 2.     gameobject cube = gameobject.createprimitive(primitivetype.cube); 3.     cube.transform.position = new vector3(0, 0.5f, 0); 4. } else if (action == "rotate") { 5.     var x = convert.toint32(message.args["rotatex"]); 6.     var y = convert.toint32(message.args["rotatey"]); 7.     cube.transform.rotate(x,y,0); 8. } 

this proof of concept. won't need handle multiple cubes this. how should instantiate in proper context (above if statement) without "placing" in scene?

you should declare object first, , rotate if value not null. this

gameobject cube = null;  if (action == "place") {     cube = gameobject.createprimitive(primitivetype.cube);     cube.transform.position = new vector3(0, 0.5f, 0); } else if (action == "rotate") {     var x = convert.toint32(message.args["rotatex"]);     var y = convert.toint32(message.args["rotatey"]);     if(cube != null)         cube.transform.rotate(x,y,0); } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -