Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

0

The answer to the question is: ScriptableObject. That's what they're for. Put your variables in a ScriptableObject and Unity will handle the serialisation and give you a custom editor and other nice features. Recommended.


1

Here are four ways to solve this problem... First: In the general case, the easiest way to adjust the rotation of something in Unity is to wrap it. Create a new empty GameObject called "parent" Take the visual object you want to rotate and make it a child of this parent. Take whatever animation/script/etc had been manipulating the visual object directly, ...


0

With regard to your question: "The surprising thing was for me that why it is creating problem at position 17000 of x-axis while unity allow value less than 1,000,000 (not restricted to it actually)." jitter starts to occur earlier than you might expect because the resolution gap of floating point increases (gets worse) almost immediately as you move away ...


0

You can do this with a MaterialPropertyBlock, just like you would in the old pipeline to reduce the overhead of instantiating modified copies of materials. The only difference is that the shader property is called "_BaseColor" not "_Color": // You can re-use this block between calls rather than constructing a new one each time. var block = new ...


0

I've tried matName.CopyPropertiesFromMaterial(otherMatName); and it worked! Creating another materials isn't the best way but in my case i've already had those materials and this way was the solution for me.


2

The short answer is no. The longer answer is noooooooo. Well, it’s actually “almost, but...” You seem to be mistrustful of Unity and want to do everything manually. Unity is designed to be the opposite of that. Sounds like Unity is not the engine for you in that case. Some reasons why what you want to do is not a good idea: technically impossible. On the ...


1

It sounds like what you want to do is create a prefab, and load that dynamically from a script. I believe to execute a script you will need an object to which it can be attached. You can add a public field to your script which accepts a GameObject, attach that script to a game object, and drag-and-drop your prefab from the project explorer on to the ...


0

Here is how I draw something (object B) at a specific position of something else (object A). First, you need to have a method (or something) to get the X and Y position of object A: public float getPosX() { return pos.X; } Then you simply set the position of your object B in Update method as below: objectB.setPos(objectA.getposX() + offsetX, objectA....


0

Since the map projection is dependent on complex parameters not exposed to us, a formula-based exact solution seems unlikely. Instead, I'd recommend the game developer's favourite tool: piecewise approximation! This is how we handle textures, 3D models, time and physics, and it works for maps too. To simplify the problem, we'll assume y = elevation (you ...


0

I can't see anywhere you actually use the value that's been assigned to TankManager's string Text property. Moreover, assigning to the same variable repeatedly in a loop like this would result in each iteration's value getting stomped by the next one, so that only the final iteration's value remains at the end of the method. Probably not what you want? ...


0

void gameover() { Vector3 positions = cams.transform.position + new Vector3(-12, 0, 0); } This code creates a temporary variable called positions and assigns it a value 12 units to the left of the camera's position. It then does nothing whatsoever with that variable, and exits. If you want to move the camera, then you can assign your new shifted ...


0

It still doesn't look like we have enough information here to identify why your game behaves differently on Android, but I'd like to recommend we start with a clean slate to hopefully make the code simpler to understand, more concise, with less room for bugs to creep in. I'll build this up in pieces so it's easier to follow: First, let's generate your plane ...


1

UPDATE: (5/18/2019) If you are looking for how to get vertices (and other data like its range from 0 to 1, or its center point) of every cell generated in a Voronoi diagram, I would suggest using a Delaunay triangulation library. Coding this by hand is possible too, but may not be worth your time. This one looks promising for Unity users: csDelaunay Here ...


0

To find the distance of our point \$\vec P\$ to the line segment between points \$\vec A\$ and \$\vec B\$, we need the closest point on the line segment. We can represent such a point \$\vec Q\$ on the segment by: $$\vec Q = \vec A + t (\vec B - \vec A)$$ for some scalar value \$ 0 \le t \le 1 \$, which ensures our point doesn't shoot off the ends of the ...


0

The simplest way is to give your prefab a suitable collider, and attach a script to the prefab that looks a bit like this: public class TouchDetector : MonoBehaviour { // Called when a collider attached to this game object is pressed. void OnMouseDown() { // Put the actions you want to perform to respond to the touch here. Debug....


0

I've done this in my game controller script (but you don't have to). Whichever makes more sense to you, I think. One method you can use is OnTriggerEnter. Your could use code that goes something like the below. It will let you tag all the objects with one tag but you can do something with each individually -- if you want to do the same thing with each ...


-1

First of all, because I don't have a compiler right now: List<GameObject[]> gList=new List<>();//this may have some problem, try initiating in your way GameObject[] line1=new GameObject{g1,g2,g3}; //g-n is just some gameobject GameObject[] line2=new GameObject{g4,g5,g6,g7}; gList.add(line1); gList.add(line2); GameObject[] targeted=null; ...


0

There are some errors: Mybutton.GetComponent<Button>().GetComponent<MyScript>().MyFunction(); MyButton is correctly referenced ? If Yes, just use: MyButton.GetComponent().MyFunction(); To get MyButton reference use: GameObject MyButton = GameObject.Find("MyButton");


0

Alright my dude. Welcome to the hell that is layout groups. Here's a few things you'll need to know first: If nothing is sizing the object, the layout group will not work as you expect (in fact it appears the opposite). Things that size the object include LayoutElement and ContentSizeFitter. For whatever reason the default rect transform sizings make the ...


1

In your code you have tied the game logic of the player directly to the motion of the camera; this creates a situation where the player and the camera play catch-up. Thus the player's "screen" position dictates the behavior, while actually this should be the "world" position. The best way to solve this, is to untie the camera to the game logic. Basically, ...


0

I think the issue was that I was additively loading scenes and the sphere I was initially highlighting was in the active scene and I was unhighligthing the sphere by finding an object of the same name and there was a sphere in another loaded scene. I guess it was unhighlighting the sphere in the other scene even though that wasn't the active scene.


1

Store the value every frame but before that check if it changed: if (distPlayer5 < distPlayer2 && distPlayer5 < distPlayer1 && distPlayer5 < distPlayer4 && distPlayer5 < distPlayer3 && distPlayer5 < distPlayer6 && distPlayer5 < distPlayer7) { //... } //... if(lastPathPoint != pathPoint){ ...


1

Here. rWhirlpool = whirlpool; whirlpool is not your current whirlpool, whirlpools[i] is. That's why you look at one and the same whirlpool which comes as a parameter. You don't need that parameter at all. Also, look at this: return target; } // the brace from for loop You always return after the first iteration. So you will never process more that ...


0

This sounds a like a situation where it might be useful to use an eventsystem/callbacks/delegates. Do some research on those keywords and see, if something like that fits your project better. It might be a little more work at the beginning, because you might need to learn something new, but I think it's the better way to go than to fix a problem, which you ...


0

It should be a coroutine too: public IEnumerator PlayConversations () { for (int i = 0; i < 5; i++) { yield return StartCoroutine (PlayConversation (_conversations[i])); } } Call it: StartCoroutine (PlayConversations ()); And it will call 5 PlayConversation coroutines one after another.


1

The #if preprocessor directive can be used to check if specific preprocessor symbols are defined or not. Those symbols include certain predetermined ones (such as DEBUG) as long as whatever you define with #define or the -define switch to the compiler itself. You cannot use the preprocessor to check for the existence of a field or any other sort of member ...


0

Can you provide some screenshots of the problem? Just try this method below, I hope it can solve your problem: public HitState checkHit(Rectangle r1, Rectangle r2) { HitState retv; retv.hit = false; retv.hitTop = false; retv.hitBottom = false; retv.hitLeft = false; retv....


1

I found out that I had disabled "Queries hit triggers" on the Physics 2D configuration section. Enabling it solved my problem.


0

"I looked all over the internet" and somehow completely missed the enabled checkbox? ;) [RequireComponent(typeof(Renderer))] public class HideShowOnClick : MonoBehaviour { Renderer visual; void Start() { visual = GetComponent<Renderer>(); } void OnMouseDown() { // If it was shown, hide it. If it was hidden, show it. ...


0

There is a little problem with the code. When you press the button "UseVR" PlayerThree does follow PlayerTwo but only for as long as the button is pressed. As you stop pressing the button, the if statement is false, and it stops following PlayerTwo, and same happens with the follow PlayerOne's if statement. So we need to insert a concept that follows the ...


0

Inside your character's Rigidbody2D component, expand the "Constraints" fold-out, and tick the "Freeze Rotation Z" box. This will ensure that physics impacts can't cause your character to spin.


0

It looks like you can solve this by keeping track of the last-placed note in each lane, then stitching the continuation of that note onto it. Let's assume you have a script on your note representation prefab(s) onto which we can add the concept of a "continuation" like what's below: public class NoteRepresentation : MonoBehaviour { public NoteKind ...


2

Old question, but looking at it this morning I noticed that the directions collection isn't defined in the body of the method, nor passed as an argument. That means, unless its iterator is defined in an unusual way, that every recursive call is using the same collection of directions, re-shuffling it on every recursion. This is a problem because this maze ...


4

The red you see in your console is not an exception, it's an internal unity error. so that try catch doesn't help. You can't destroy gameObjects in that frame. You have to delay your call until the next frame. you can achieve this with coroutines. using System; using System.Collections.Generic; using System.Collections; using UnityEngine; [...


0

There can be issue to the layers. Here you can check out edit->Project settings->physics. See there which layer is colliding and which is not.


1

Silly mistake : GameObject tem = GameObject.CreatePrimitive(PrimitiveType.Cube); tem.transform.position = point; added replacing GameObject tem = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Cube), point, Quaternion.identity); 2 Cubes were created on that line every time.


0

None of the above worked for me. What did work for me was to Add a new dummy class to the folder (example: "DeleteMe"). Then try to reference that new dummy class somewhere. When I did this, Intellisense actually saw this dummy class (when it wasn't seeing any of my existing classes). After this, I was able to access the original class I was trying to ...


2

I finally used this code : using UnityEngine; using System.IO; using System; public class Capture : MonoBehaviour { private static int resWidth = 3840; private static int resHeight = 2160; private static GameObject camObj = null; private static RenderTexture rt; private static Texture2D screenShot; public static string ...


4

Textures, RenderTextures, and Materials use memory outside of C#'s managed heap (including GPU memory), so the regular C# garbage collector alone can't fully clean them up. You need to manually Destroy() them once you no longer need them. Your current code does this for the RenderTexture rt (though it doesn't call Release first - I'm not sure if that's ...


0

The problem was I was changing the value of resHeight and resWidth as @DMGregory mentioned. So it was rendering in poor quality. Now, using UnityEngine; using System.IO; using System; //getty code public class Capture : MonoBehaviour { private static int resWidth = 3840; private static int resHeight = 2160; private static GameObject camObj = null; public ...


1

You can take a look at globalstats.io (https://globalstats.io). It is a hobby project I am running for some time now that offers free leaderbords for your exact use case.


1

You're problem is that your try to change a value-type object and expect to change something. It's the same when you want to change the position. See this example: // this wouldn't work Vector3 position = gameObject.transform.position; position = new Vector3(position.x + 1, position.y, position.z); // this would work Vector3 position = gameObject....


0

Nevermind. I solved it. The problem was the order in which I assigned the speed to the Nav Mesh Agent component in code. (The code that is commented out in the code above) I had it written as randSpeed = navMeshAgent.speed; It should have been navMeshAgent.speed = randSpeed; Also, since it is in the Start function, make sure that you've set the speed ...


0

Okay, fixed this with /* * Postpones the ineraction with the clicked on object until the player * has arrived. */ void WaitUntilArrivalToInteract(Vector3 clickedWorldPosition, TileBase clickedGroundTile) { atDestinationCoroutine = WaitAndHandleClickInteraction(clickedWorldPosition, ...


0

Have you tried this overload? public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);


0

A first-person camera is one that is always situated where a playable character's head/eyes would be, and thus always perceives the world from the perspective that that character would. This is in contrast to a third-person camera, for example, which is typically situated somewhere else in the world and sees a playable character from an external perspective. ...


0

The object was far from the origin i.e., it was creating spatial Jitter (SJ) or wobble effects in mesh. The surprising thing was for me that why it is creating problem at position 17000 of x-axis while unity allow value less than 1,000,000 (not restricted to it actually). The problem has solve through floating origin technique, the general idea is that : You ...


4

GameObjects do not have children per-se. When we talk about children to a GameObject we are actually referring to the children of the Transform component (that all GameObjects have). This means that you have to use the Transform component to access the children: void Start() { // All GameObjects have a transform component built-in foreach(Transform ...


1

you can use transform.GetChild(0) you can also get children of children transform.GetChild(0).GetChild(0) or you can pull a array of each child and work with that also for (int i = 0; i < transform.childCount; i++) { //do stuff } or foreach (Transform child in transform) { // do stuff }


0

transform.Translate says "move me by this offset now" It does not say "take the object I just spawned, and start moving it at this velocity, every frame from now into the indefinite future" Really, you should put the movement behaviour on the object you've spawned, not on the object that spawns it, as I described to you in a previous answer. public class ...


Top 50 recent answers are included