Questions tagged [unity-components]

Components are scripts added to a GameObject in Unity3d.

In Unity you can create GameObjects in a scene and attach scripts to them that include game logic and extend MonoBehaviour. A script is written in C#, javascript or boo and becomes a component when attached to a GameObject.

44 questions
5
votes
1 answer

How do I mask a custom UI Graphic in Unity?

In Unity 2017, I've managed to draw vector graphics by making my own UI behaviors that inherit from UnityEngine.UI.Graphic. But my code is irrelevant because you can observe this problem just as well using the SimpleImage example from the…
Kyle Delaney
  • 11,616
  • 6
  • 39
  • 66
4
votes
2 answers

How do I change the source image of a Unity image component?

I can't figure out how to change the sprite used for the source image. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; public class SceneButton : MonoBehaviour { public Sprite…
Jaedon KLB
  • 67
  • 1
  • 1
  • 6
4
votes
2 answers

How do I enable Parts/Components in Unity C# with only a game object in the script

I am using Photon to make put multiplayer in my game, to ensure that one player doesn't control them all, when you spawn in, client side it will activate your scripts/camera so you can see and move. Although I can't think of a way around this…
user4540474
4
votes
1 answer

Coroutine in Extension Method

I wrote an extension method to make an object move over time. It works well; however, because the object is performing that task over a period of time, it is ignoring all other calls, such as my update method. I am assuming I need to do something…
Evorlor
  • 7,263
  • 17
  • 70
  • 141
3
votes
1 answer

Unity3d AddComponent taking variable

Details: In C# on Unity3d Searched on the unity documentation but the example doesn't fit to my case, same for other websites i visited. (i did not list them as i should.) What i am trying to do is passing a class variable with a constructor to be a…
Po0ka
  • 107
  • 3
  • 9
3
votes
1 answer

Call a function on AddComponent( string variable )

Type thistype = stringVar.GetType(); thistype myScript = gameObject.AddComponent(stringVar); myScript.runCustomFunction(); This doesn't work, and I believe it is because I cannot cast to a variable type if I don't know the variable type at compile…
2
votes
2 answers

Unity UI - How to make a "Composite" Layout Group to combine multiple images in the same location?

I'm making a simple auto-layout UI that consists of a Panel with a background image and three rows of text. To start with, I have a Vertical Layout Group component in a top-level "Panel" GameObject, set to Control Child Height. It also has a Content…
davidA
  • 12,528
  • 9
  • 64
  • 96
2
votes
5 answers

Using Nested Class as Unity3D Component

I am using Unity 5.4.2f2 Personal, and I have written a C# script similar to the following: using UnityEngine; using System; public class Outer : MonoBehaviour { // Some public outer fields public class Inner : MonoBehaviour { //…
Rabadash8820
  • 2,328
  • 3
  • 27
  • 49
2
votes
2 answers

Accessing a runtime assigned Object from another component?

As it stands I can't seem to make sense of the events in my program. I have two components. One called EnemyMeleeSwitching and one called EnemyAI. EnemyMeleeSwitching, who's script is contained in the same parent level as EnemyAI at runtime selects…
2
votes
1 answer

OnGUI is called just in first component

I'm creating a game to learn about Unity watching a tutorial (this one) and my Player has two components: PlayerHealth and PlayerAttack. The both are working fine as well but the problem is when I tried to do something in a OnGUI of second component…
Idemax
  • 2,712
  • 6
  • 33
  • 66
2
votes
1 answer

Access a variable from another script using GetComponent

In unity (the game engine) I have two scripts CharacterSelect.js CarCameraScript.js I want to access the variable selectedPlayer FROM characterSelect.js IN CarCameraScript.js That variable currently looks like this: var selectedPlayer : int =…
1
vote
0 answers

Why, if in the debug mode you would pass through the line AudioSource.Play, playback does not start immediately after the line is executed?

Why, if in the debug mode you would pass through the line AudioSource.Play(); playback does not start immediately after the line is executed? public AudioSource inactiveMusic; void Start() { inactiveMusic.Play(); } But if the line…
1
vote
1 answer

TextMeshPro Text only renders on some part of the game screen

I am trying to add TextMeshPro on a game object(sprite) outside the UI in 2D world. So I created a sprite to act as the background and parent game object then I put a canvas as the child of the sprite. Finally I put the TextMeshPro as the child of…
Kenart
  • 285
  • 3
  • 14
1
vote
1 answer

Unity - The Animator won't let not me and not the code to disable a collider. Why?

I have a character game object, with both an animator and a collider. Whenever the animator is on, the collider cannot be changed during run-time, though it can be changed in scene editing mode, via the inspector. No matter what animator properties…
Blawnode
  • 13
  • 1
  • 5
1
vote
1 answer

Get ?-type Collider from child, and add duplicate component to parent

I'm trying to get a Collider of unknown type from a child object at run-time and add an identical Collider to the parent. How can I do that? This is the best I've got so far, but doesn't work: Collider MColl =…
Spectralle
  • 69
  • 1
  • 8
1
2 3