how to GetComponent/constantForce.force from another script

hi, i’m new! to scripting! i’ve zero knowledge on howto script! but i learn fast!

im at a roadblock, i wouldn’t be posting in the forums if i didn’t have to, but after all the wasted time now i’ve run out of time to figure this step out, and must move on, so i need to get moving!

hopefully i can get some fast help here.

and, my other question (unrelated to this topic) is what is the best way to search this forums? like how do the keywords work here, do i type in a “function” or “how to cal constantforce from another script”, because i keep getting results from all sorts of posts, it’s very very draining on me, and my time, to clik through each topic and search for my answer…i keep getting 12-18pages of results. i’m using Answers.Unity as well but the responses are not coming quick enough for me to get my work done. youtube search is not getting me anywhere much, and my past 30days have seen pretty much zero to 1% progress, in terms of the scripting…the art is doing well ahead of pace.

ok, here’s what i have now, attached to just the standard GameObject > Create > Cube, and a Component > Constant Force applied as well.

so i have:

using UnityEngine;
using System.Collections;

public class engine : MonoBehaviour {

	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown("a")){
			print("A was pressed to move left");
			transform.Translate(Vector3.left * 10);
		}
		
		if (Input.GetKeyDown("d")){
			print("D was pressed to move right");
			transform.Translate(Vector3.left * -10);			
		}
		
		if (Input.GetKeyDown("x")){
			print("X was pressed");
			constantForce.force = Vector3.forward * 1000;
		}
	}
	
}

on another GameObject > Cube, i set “is Trigger” to true (checked).

when the first cube passes over/through this 2nd cube, i want the “ConstantForce” value that is on the first cube to increase to a new value.

here’s what i wrote: (the comments are there because i gave up trying to fix my error and needed to save my progress and “Play” test to run, and i have no clue what the unity console is saying to me the error is).

using UnityEngine;
using System.Collections;

public class booster : MonoBehaviour {
	//public GameObject target;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	//void OnTriggerEnter (Collider other) {
	//	Boost();		
	//}
	
	//private void Boost(){
		//engine e = (engine)target.GetComponent<engine>();
	//	ConstantForce = GetComponent<engine>();
	//	constantForce.force = Vector3.forward * 3000;
	//}
}

big thanks to anyone that can help, and fast, and thanks for reading my topic!!

how can i apply his reply from another topic, to my trouble i’m having? in csharp too, his topic appears to be java.
i just tried but unity is giving me parsing errors and Type stuff, i tried rewriting my on my own, gotten different kinds of errors so can’t post the xact error now.

http://forum.unity3d.com/threads/74879-Find-a-variable?p=479324&viewfull=1#post479324

var playerObject = GameObject.Find("player name here"); //obtain a reference to the object with the script attached
var lifeScript = playerObject.GetComponent<LifeScript>(); //obtain a reference to the script itself
var currentLife = lifeScript.life; //copy the value of the life variable to a local variable

again, im trying to get the “Constant Force” world Force in Z value, from the first script/cube, to my 2nd cube script.

Welcome to the world of game development :slight_smile: I’ve gone through 12-18 pages of forum threads many times looking for info I needed, and I’m sure many others have as well :wink: Unfortunately, research and investigation of that sort is often part of the game development process, and can’t always be avoided.

So I assume the commented-out parts of the code you posted are generating errors? If so, you should always post the actual error messages you’re getting when asking for help with them. I will say though that this line:

ConstantForce = GetComponent<engine>();

Is probably incorrect. If ConstantForce is a type (e.g. a built-in Unity script/component), then you’ve left out the variable name. What you (most likely) want is:

ConstantForce constantForce = GetComponent<engine>();

[Edit: Actually, that’s not right either, because you’re requesting a component of type ‘engine’ and trying to assign it to a variable of type ConstantForce (sorry, didn’t notice the ‘engine’ part). Again, it would help if you could post the actual unedited code and the error messages it’s generating.]

heya Jesse, !

yeah thanks so much, i know i should post the errors too and the parts of code that is actually creating problems, but i kept rewriting/editing mine to the one i posted above that ive lost track on what was causing probs because i kept switching (trial-and-error) so much. i will be posting accordingly from here out :stuck_out_tongue:

anyways, i think i should also have mentioned, what i’m trying to do afterall.
i want my playerobject (eg. a boat) to be moving forward constantly…and it can “get powerups” (OnTriggerEnter) that will increase or decrease that initial velocity or speed setting.
so, i have 2 gameobjects, i’m currently choosing to write 2 scripts, one attached to each object.

perhaps this explanation, alongside my top post, will help with me getting the right guidance too =)!

Sure, there should be no problem doing that. In order to help though, it would still help to see the actual code and errors. If you could just re-create one of your previous attempts (or just take another run at it from scratch), and then post the code along with any errors you’re getting and/or a description of how it’s not working, that’d give us something concrete to work with.

Meanwhile, I’ll just direct you to this page, which provides some examples of how to communicate between objects in Unity. (It sounds like you just want to access one component in a game object from another component in that same object, but at least some of the concepts involved are fairly similar.)

yep, gone through that whole page too already, it’s also another reply that another person gave to another guy asking too.

but im not familiar enough with scripting to apply what im reading without getting errors.

i posted my code already, in the top post, its the most current too, i wrote all that from all that i’ve absorbed during my researching and experimenting.

end of the day now for me over here this side of the world, i’ll try recreating from start tomoro…

Oh, ok. Well, I guess I’m not 100% sure what the question is.

Maybe someone else will be able to help in the meantime, but if you end up finding you’re still stuck on this, maybe you could post back here and restate the question, just for clarity’s sake.